.htaccess RewriteRule 在子目录中不起作用 [英] .htaccess RewriteRule not working in subdirectory
问题描述
我正在为我的网站的新版本编程,我正在尝试让 .htaccess 正确重写.我的新网站存储在这里:
I'm programming the new version of my website and I'm trying to get .htaccess to rewrite properly. My new site is stored here:
www.example.com/storage/new/
我需要重写这些 URL:
I need to rewrite these URLs:
www.example.com/storage/new/welcome/ -> index.php?action=welcome
www.example.com/storage/new/page/name/ -> index.php?action=page&url=name
www.example.com/storage/new/post/name/ -> index.php?action=post&url=name
这是我的 .htaccess 文件:
This is my .htaccess file:
RewriteEngine On
RewriteRule ^/welcome/$ index.php?action=welcome [L]
RewriteRule ^/page/([a-zA-Z0-9]+)/$ index.php?action=page&url=$1 [L]
RewriteRule ^/post/([a-zA-Z0-9]+)/$ index.php?action=post&url=$1 [L]
然而,它不起作用;所有结果都是 404 Not Found.我什么都试过了,甚至用 www.example.com/storage/new/
代替 ^
.我在服务器根目录 (www.example.com
) 中有另一个 .htaccess,如下所示:
It does not work, however; all results in a 404 Not Found. I've tried everything, even typing out www.example.com/storage/new/
in lieu of ^
. I have another .htaccess in the server root (www.example.com
) that looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
我无法想象这会如何影响 www.example.com/storage/new/
但你永远不知道.有人能帮我解决这个问题吗?
I can't imagine how that would affect www.example.com/storage/new/
but you never know. Can anyone help me with this?
推荐答案
我不得不通过电子邮件向服务器管理员寻求帮助,结果发现 .htaccess 将自己的路径视为 root;我只是删除了每个规则中 ^
之前的第一个 /
.我的最终 .htaccess 文件如下所示:
I had to e-mail my server's administrator for help and it turns out that .htaccess treats its own path as root; I simply removed the first /
before the ^
in each rule. My final .htaccess file looks like this:
RewriteEngine On
RewriteRule ^welcome/$ index.php?action=welcome [L,QSA]
RewriteRule ^page/(.*)/$ index.php?action=page&url=$1 [L,QSA]
RewriteRule ^post/(.*)/$ index.php?action=post&url=$1 [L,QSA]
这篇关于.htaccess RewriteRule 在子目录中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!