通过删除参数问号来重定向某些子文件夹 [英] Redirect certain subfolders by removing the parameter question mark

查看:63
本文介绍了通过删除参数问号来重定向某些子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.htaccess重定向域的某些子文件夹,以删除问号以改善我的网址.

I am using .htaccess to redirect certain subfolders of my domain, to remove the question mark to improve my URLs.

目前,我的网址是这样的:

Currently my URLs are like this:

www.example.com/post/?sometitle

我正在尝试删除问号,所以它是以下URL:

I am trying to remove the question mark, so it is the following URL:

www.example.com/post/sometitle

当前,我的.htaccess文件中包含以下代码:

Currently I have the following code in my .htaccess file:

RewriteCond %{THE_REQUEST} /post/?([^\s&]+) [NC]
RewriteRule ^ /post/%1 [R=302,L,NE]

推荐答案

我正在使用php GET参数,当浏览器访问example.com/post/sometitle时,我正在尝试显示当前example.com/post/?sometitle的页面

i am using php GET parameters, i am attempting for when the browser visits example.com/post/sometitle that the page that is currently example.com/post/?sometitle is displayed

在这种情况下,您需要与所问的问题相反:您需要内部重写(而不是外部重定向")来自example.com/post/sometitleexample.com/post/?sometitle.

In that case you need to the opposite of what you are asking in your question: you need to internally rewrite (not externally "redirect") the request from example.com/post/sometitle to example.com/post/?sometitle.

但是,您必须已经更改了应用程序中的所有URL才能使用新的URL格式(没有查询字符串).您不应该单独使用.htaccess.

However, you must have already changed all the URLs in your application to use the new URL format (without the query string). You shouldn't be using .htaccess alone for this.

我还假定/post是物理目录,并且您实际上在该目录中服务index.php(mod_dir向该文件发出内部子请求).因此,实际上是/post/index.php?sometitle而不是/post/?sometitle

I also assume that /post is a physical directory and that you are really serving index.php in that directory (mod_dir is issuing an internal subrequest to this file). So, instead of /post/?sometitle, it's really /post/index.php?sometitle?

例如:

RewriteEngine On

# Rewrite /post/sometitle to filesystem path
RewriteRule ^post/([\w-]+)$ /post/index.php?$1 [L]

因此,现在,当您请求/post/sometitle时,该请求将由内部重新编写并由/post/index.php?sometitle处理.

So, now when you request /post/sometitle the request is internally rewritten and handled by /post/index.php?sometitle instead.

我假设"sometitle"可以由1个或多个字符a-zA-Z0-9_-组成.因此,正则表达式[\w-]+.

I have assumed that "sometitle" can consist of 1 or more of the characters a-z, A-Z, 0-9, _ and -. Hence the regex [\w-]+.

如果这是一个新站点,则可以在那里停下来.但是,如果您要更改现有的URL结构,该结构已被搜索引擎索引并已由外部第三方链接,则您需要将旧的URL 重定向. (重申一下,您必须已经更改了应用程序中的URL,否则用户在浏览您的网站时会遇到反复的重定向.)

If this is a new site then you can stop there. However, if you are changing an existing URL structure that has already been indexed by search engines and linked to by external third parties then you'll need to redirect the old URLs to the new. (Just to reiterate, you must have already changed the URL in your application, otherwise users will experience repeated redirects as they navigate your site.)

要实现重定向,您可以在上述重写之前的 之前添加以下内容:

To implement the redirect, you can add something like the following before the above rewrite:

# Redirect any "stray" requests to the old URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ([\w-]+)
RewriteRule ^post/$ /post/%1 [R=302,NE,QSD,L]

REDIRECT_STATUS环境变量的检查是为了确保我们仅重定向直接请求",从而避免重定向循环.

The check against the REDIRECT_STATUS environment variable is to ensure we only redirect "direct requests" and thus avoiding a redirect loop.

(仅在测试为"OK"时才更改为301,以避免出现缓存问题.)

(Change to 301 only when tested as OK, to avoid caching issues.)

摘要:

RewriteEngine On

# Redirect any "stray" requests to the old URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ([\w-]+)
RewriteRule ^post/$ /post/%1 [R=302,NE,QSD,L]

# Rewrite /post/sometitle to filesystem path
RewriteRule ^post/([\w-]+)$ /post/index.php?$1 [L]

更新:如果您有多个都遵循相同模式的URL(文件夹"),例如/post/<title>/home/<title>/build/<title>,则可以修改以上内容以适应对于所有三个,例如:

UPDATE: If you have multiple URLs ("folders") that all follow the same pattern, such as /post/<title>, /home/<title> and /build/<title> then you can modify the above to cater for all three, for example:

# Redirect any "stray" requests to the old URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ([\w-]+)
RewriteRule ^(post|home|build)/$ /$1/%1 [R=302,NE,QSD,L]

# Rewrite /post/sometitle to filesystem path
RewriteRule ^(post|home|build)/([\w-]+)$ /$1/index.php?$2 [L]


旁边:(戴上我的网站管理员 ...) URL结构的改进".如果这是一个已建立的网站,具有许多反向链接和良好的SE排名,那么您应该三思而后行,因为至少在最初时您会看到排名有所下降.


Aside: (With my Webmasters hat on...) This is not really much of an "improvement" to the URL structure. If this is an established website with many backlinks and good SE ranking then you should think twice about making this change as you could see a dip in rankings at least initially.

这篇关于通过删除参数问号来重定向某些子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆