问题 .htaccess RewriteRule在子目录中不起作用


我正在编写我的网站的新版本,我正试图让.htaccess正确地重写。我的新网站存储在这里:

www.example.com/storage/new/

我需要重写这些URL:

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文件:

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。我已经尝试了一切,甚至打字 www.example.com/storage/new/ 替代 ^。我在服务器根目录中有另一个.htaccess(www.example.com)看起来像这样:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

我无法想象这会如何影响 www.example.com/storage/new/ 但你永远不知道。谁能帮我这个?


7232
2018-03-31 03:06


起源



答案:


我不得不通过电子邮件向我的服务器管理员发送电子邮件以获得帮助,结果证明.htaccess将自己的路径视为root;我只是删除了第一个 / 之前 ^ 在每个规则中。我的最终.htaccess文件如下所示:

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]

10
2018-04-04 20:19





^表示字符串的开头。 RewriteRules将查看example.com/之后的所有内容,因此您需要在模式中包含storage / new /(或删除^)。

另外我可能想要添加NC标志,这样你的模式就可以匹配而不考虑区分大小写(例如/ Page /或/ page /都可以)。这意味着您可以将[a-zA-Z0-9]模式更改为[a-z0-9]

RewriteRule ^storage/new/welcome/$ index.php?action=welcome [L,NC]
RewriteRule ^storage/new/page/([a-z0-9]+)/$ index.php?action=page&url=$1 [L,NC]
RewriteRule ^storage/new/post/([a-z0-9]+)/$ index.php?action=post&url=$1 [L,NC]

2
2018-03-31 11:16



感谢您的回复,尽管它仍然不起作用。我已经尝试了所有我能想到的东西,并阅读了比我能算的更多的教程。什么可能是错的? - Steven Zezulak
好的,尝试添加重写并确保你最终得到你期望的URL:RewriteRule ^ storage / new / welcome / $ index.php?action = welcome [L,NC,R = 301] - duncan
依然没有。肯定还有其他错误。我会提交支持票。感谢你的帮助! - Steven Zezulak


答案:


我不得不通过电子邮件向我的服务器管理员发送电子邮件以获得帮助,结果证明.htaccess将自己的路径视为root;我只是删除了第一个 / 之前 ^ 在每个规则中。我的最终.htaccess文件如下所示:

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]

10
2018-04-04 20:19





^表示字符串的开头。 RewriteRules将查看example.com/之后的所有内容,因此您需要在模式中包含storage / new /(或删除^)。

另外我可能想要添加NC标志,这样你的模式就可以匹配而不考虑区分大小写(例如/ Page /或/ page /都可以)。这意味着您可以将[a-zA-Z0-9]模式更改为[a-z0-9]

RewriteRule ^storage/new/welcome/$ index.php?action=welcome [L,NC]
RewriteRule ^storage/new/page/([a-z0-9]+)/$ index.php?action=page&url=$1 [L,NC]
RewriteRule ^storage/new/post/([a-z0-9]+)/$ index.php?action=post&url=$1 [L,NC]

2
2018-03-31 11:16



感谢您的回复,尽管它仍然不起作用。我已经尝试了所有我能想到的东西,并阅读了比我能算的更多的教程。什么可能是错的? - Steven Zezulak
好的,尝试添加重写并确保你最终得到你期望的URL:RewriteRule ^ storage / new / welcome / $ index.php?action = welcome [L,NC,R = 301] - duncan
依然没有。肯定还有其他错误。我会提交支持票。感谢你的帮助! - Steven Zezulak