没有从一个文件夹重定向(重写)到另一个文件夹
No redirection (rewrite) from one folder to another folder
完整的 .htaccess 文件
以下是我的完整 htaccess 文件。
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#redirect index to homepage
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#prevent hotlinking
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?www.example.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
#redirect galleries to portfolio
RewriteRule ^galleries/(.*)$ /portfolio/ [R=301,NC,L]
#prevent access to wp-config
<files wp-config.php>
order allow,deny
deny from all
</files>
</IfModule>
有问题的指令
下一张显然是有问题的:
#redirect galleries to portfolio
RewriteRule ^galleries/(.*)$ /portfolio/ [R=301,NC,L]
预期结果
如果我转到 http://subdomain.example.com/galleries/headshots/
,服务器应该重定向到 http://subdomain.example.com/portfolio/headshots/
发生了什么
什么都没发生,我留在http://subdomain.example.com/galleries/headshots/
。
到目前为止我已经尝试过但没有成功的方法
我在源和目标的末尾都添加了斜杠 - 没有任何变化:
#redirect galleries to portfolio
RewriteRule ^galleries/(.*)$/ /portfolio// [R=301,NC,L]
还尝试将指令放在顶部,就在 RewriteBase /
下方 - 仍然没有成功。
备注
请注意,这发生在子域上(参见示例 url),但我认为这不会对此行为产生影响。
这是在安装 Wordpress 时发生的,但我怀疑它会影响它。所有其他指令似乎都可以正常工作(对更改做出反应)。
编辑(来自对@Panama Jack 的评论回复):两个目录实际上都存在并且是单独的页面,但如果您愿意,一个仅设置为脚本(其他页面必须访问其图库)。
在您的重写规则中,URL 应该以 / 开头。
此外,(.*)$/
部分很奇怪:你先找任何东西直到最后,然后加一个斜杠。您想将重写规则限制在目录中吗?那么为什么不在模式中包含尾部斜线:(.*)/$
?
以下规则将重定向 下面的所有内容 /galleries/
RewriteRule ^/galleries/(.*)$ /portfolio/ [R=301,NC,L]
斜杠位置错误。改变这个
RewriteRule ^galleries/(.*)$/ /portfolio// [R=301,NC,L]
至此
RewriteRule ^galleries/(.*)/$ /portfolio// [R=301,NC,L]
完整的 .htaccess 文件
以下是我的完整 htaccess 文件。
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#redirect index to homepage
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#prevent hotlinking
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?www.example.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
#redirect galleries to portfolio
RewriteRule ^galleries/(.*)$ /portfolio/ [R=301,NC,L]
#prevent access to wp-config
<files wp-config.php>
order allow,deny
deny from all
</files>
</IfModule>
有问题的指令
下一张显然是有问题的:
#redirect galleries to portfolio
RewriteRule ^galleries/(.*)$ /portfolio/ [R=301,NC,L]
预期结果
如果我转到 http://subdomain.example.com/galleries/headshots/
,服务器应该重定向到 http://subdomain.example.com/portfolio/headshots/
发生了什么
什么都没发生,我留在http://subdomain.example.com/galleries/headshots/
。
到目前为止我已经尝试过但没有成功的方法
我在源和目标的末尾都添加了斜杠 - 没有任何变化:
#redirect galleries to portfolio
RewriteRule ^galleries/(.*)$/ /portfolio// [R=301,NC,L]
还尝试将指令放在顶部,就在 RewriteBase /
下方 - 仍然没有成功。
备注
请注意,这发生在子域上(参见示例 url),但我认为这不会对此行为产生影响。
这是在安装 Wordpress 时发生的,但我怀疑它会影响它。所有其他指令似乎都可以正常工作(对更改做出反应)。
编辑(来自对@Panama Jack 的评论回复):两个目录实际上都存在并且是单独的页面,但如果您愿意,一个仅设置为脚本(其他页面必须访问其图库)。
在您的重写规则中,URL 应该以 / 开头。
此外,(.*)$/
部分很奇怪:你先找任何东西直到最后,然后加一个斜杠。您想将重写规则限制在目录中吗?那么为什么不在模式中包含尾部斜线:(.*)/$
?
以下规则将重定向 下面的所有内容 /galleries/
RewriteRule ^/galleries/(.*)$ /portfolio/ [R=301,NC,L]
斜杠位置错误。改变这个
RewriteRule ^galleries/(.*)$/ /portfolio// [R=301,NC,L]
至此
RewriteRule ^galleries/(.*)/$ /portfolio// [R=301,NC,L]