无法重写“http://www”到“http://”在 .htaccess 中
我有一个网站,正在尝试将所有带有“http://www...”的 url 重写为“http://...”
这是我的 .htaccess 的内容
<IfModule mod_rewrite.c>
Options +FollowSymlinks
Options -Multiviews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*) http://example.com/$1 [QSA,R=301]
#RewriteBase /employers/
RewriteRule ^([a-zA-Z0-9]+)/?$ employers/page.php?page=$1 [L]
#RewriteBase /candidates/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d{4})/(0?[1-9]|1[0-2])/([^/]+)/?$ candidates/read.php?page=$1&year=$2&month=$3&slug=$4 [L]
</IfModule>
这正确地重写了这些类型的 url:www。 example.com、www.example.com/index.php
问题是它不会重写这些类型:www.example.com/candidates/、www.example.com/candidates/login.php
我该如何解决这个问题,谢谢!
编辑
我的候选人文件夹中有一个 .htaccess,内容如下:
<IfModule mod_rewrite.c>
RewriteRule ^([^/]+)/(\d{4})/(0?[1-9]|1[0-2])/([^/]+)/?$ read.php?page=$1&year=$2&month=$3&slug=$4 [L]
</IfModule>
现在,如果我对其发表评论,重写规则将正确重写为“http://”。
我的问题: 1. .htaccess 如何影响'http://'重写? 2.我该如何解决这个问题,
谢谢
I have a website and am trying to rewrite all urls with 'http://www...' to 'http://...'
This is the content of my .htaccess
<IfModule mod_rewrite.c>
Options +FollowSymlinks
Options -Multiviews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*) http://example.com/$1 [QSA,R=301]
#RewriteBase /employers/
RewriteRule ^([a-zA-Z0-9]+)/?$ employers/page.php?page=$1 [L]
#RewriteBase /candidates/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d{4})/(0?[1-9]|1[0-2])/([^/]+)/?$ candidates/read.php?page=$1&year=$2&month=$3&slug=$4 [L]
</IfModule>
This correctly rewrites these types of urls: www.example.com, www.example.com/index.php
The problem is that it does not rewrite these types: www.example.com/candidates/, www.example.com/candidates/login.php
How can i fix this thanks!
EDIT
I have a .htaccess in my candidates folder and this is the content:
<IfModule mod_rewrite.c>
RewriteRule ^([^/]+)/(\d{4})/(0?[1-9]|1[0-2])/([^/]+)/?$ read.php?page=$1&year=$2&month=$3&slug=$4 [L]
</IfModule>
Now if i comment on it, the rewrite rule rewrites properly to 'http://'.
My question:
1. How does the .htaccess affect the 'http://' rewriting?
2. How can i fix it
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我实际上测试了上面的代码,它实际上对我有用!
但我意识到,我的浏览器倾向于缓存 .htaccess 设置,即使我更改了内容也不会更新,因此我建议彻底清理浏览器缓存,然后看看会发生什么。我很确定这会有所帮助。
您还可以仔细检查,文件夹“candidtates”和“employers”下没有本地 .htaccess 文件。
I actually tested your code above, and it actually works for me!
What I realized though, is that my browser tended to cache the .htaccess settings, and not update even if I changed things, so I'd recommend making a total cleanup of the browser cache and see what happens then. I'm quite sure this helps.
What you could also double-check, is that there are no local .htaccess files under the folders "candidtates" and "employers".
我认为您需要在第一个重写规则中添加一个
L
(最后一个)。就像(这意味着重写域后 Apache 将停止处理其余规则并仅返回 301 响应)
I think you need to add a
L
(Last) to your first rewrite rule. Like(which will mean that upon rewriting the domain Apache will stop processing the rest of the rules and will just return a 301 response)
如果将两行:
...放在最后会怎样?这种改变常常为我解决问题。 (我猜“RewriteCond”如果适用于它下面的东西,它可能会把事情搞砸,这是不应该的......)
What if you place the two lines:
... at the end? This kind of change often solved things for me. (I guess the "RewriteCond" can screw things up, if it applies to things below it, which it is not supposed to ...)