mod_rewrite 强制小写 url
我在网上搜索了我的问题的答案,但没有任何运气,所以她说:
我正在重做我的 mod_rewrite 出于 SEO 目的,我希望所有网址都小写,例如:
http://BLAbla.com/
目前
http://bla.com/
我的 .htaccess 看起来像这样:
RewriteEngine on
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ([\w]*)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/]+)/([^/]?)$ index.php?worker=$1¶ms=$2 [L]
但是当我运行 http://localhost/site/ 时,我收到错误 500。我只知道基本的 reg ex,而且非常mod_rewrite 有限,所以我无法在这里看到错误。如果我删除 rewritemap 行,该页面不会给出任何错误。我的服务器不支持 rewritemap 还是什么?
非常欢迎有关 .htaccess 文件的任何其他评论:)
感谢您的宝贵时间。
i've searched the net for an answer to my problem without any luck, so her goes:
I'm redoing my mod_rewrite for SEO purposes and i want all urls to be lowercase, for instance:
http://BLAbla.com/
becomes
http://bla.com/
currently my .htaccess looks like this:
RewriteEngine on
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ([\w]*)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/]+)/([^/]?)$ index.php?worker=$1¶ms=$2 [L]
but when i run http://localhost/site/ i get error 500. I only know basic reg ex, and very limited mod_rewrite, so i cannot se the error here. If i remove the rewritemap line, the page doesn't give any error. Is rewritemap not supported by my server or something??
Any other comments regarding the .htaccess file are very very welcome :)
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Esben
重写主机名有点棘手。您有权访问服务器错误日志吗?因为这可能会使用有关 500 原因的信息。
为了清楚起见,最好将参数括在引号中:
[L]
将强制结束对此请求的处理,这意味着如果需要对 URI 进行更多工作,则会在重新提交请求时进行。您可能需要考虑批量修改,以便请求仅经历一次修改过程:以下尝试做什么?
效果(尽管有时髦的正则表达式)是“如果 REQUEST_URI 以 '
/
' 结尾,则将其剥离并重定向。”这就是你想要的吗?请记住,.htaccess
文件中的情况略有不同。并且:
看起来您试图说“将任何“foo/bar”更改为“index.php?worker” =foo¶ms=bar"'.如果这是正确的,那么它可能不会起作用。由于您位于
.htaccess
文件中,因此您将匹配文件系统路径,而不是 URI。所以试试这个:
我不确定
SKIP
对与RewriteRule
混合的RewriteCond
语句的影响,但请尝试上面的内容看看你是否更接近。@Esben
Rewriting the hostname is a bit trickier. Do you have access to the server error log? Because that may some using information about the cause of the 500.
It's a good idea to enclose the parameters in quotation marks just for clarity:
The
[L]
is going to force this to end processing for this request, which means that if there's more work to be done to the URI, it'll happen when the request is re-submitted. You might want to consider batching your modifications so requests only go through the munging process once:What is the following trying to do?
The effect (despite the funky regex) is 'If the REQUEST_URI ends in a '
/
', strip it off and redirect.' Is that what you want? Bear in mind that things are a little different in a.htaccess
file..And this:
It looks like you're trying to say 'Change any "foo/bar" into "index.php?worker=foo¶ms=bar"'. If that's correct, it's probably not going to work. Since you're in a
.htaccess
file, you're matching against the filesystem path, not the URI.So try this:
I'm not sure about the effect of
SKIP
onRewriteCond
statements mixed withRewriteRule
s, but give the above a try and see if you're any closer.