mod_rewrite 强制小写 url

发布于 2024-10-15 12:14:37 字数 844 浏览 7 评论 0原文

我在网上搜索了我的问题的答案,但没有任何运气,所以她说:

我正在重做我的 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&params=$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最近可好 2024-10-22 12:14:37

@Esben

重写主机名有点棘手。您有权访问服务器错误日志吗?因为这可能会使用有关 500 原因的信息。

为了清楚起见,最好将参数括在引号中:

RewriteMap  lc int:tolower
RewriteCond "%{REQUEST_URI}" "[A-Z]"
RewriteRule "(.*)"           "${lc:$1}" [R=301,L]

[L] 将强制结束对此请求的处理,这意味着如果需要对 URI 进行更多工作,则会在重新提交请求时进行。您可能需要考虑批量修改,以便请求仅经历一次修改过程:

RewriteMap  lc int:tolower
RewriteCond "%{REQUEST_URI}" "[A-Z]"
RewriteRule "(.*)"           "${lc:$1}" [E=REDIRECT:TRUE]

RewriteCond "%{HTTP_HOST}"   "!^www\."  [NC]
RewriteRule "^"              "-"        [E=REDIRECT:TRUE,E=PREFIX_WWW:TRUE]

以下尝试做什么?

RewriteRule ([\w]*)/$ http://%{HTTP_HOST}/$1 [R=301,L]

效果(尽管有时髦的正则表达式)是“如果 REQUEST_URI 以 '/' 结尾,则将其剥离并重定向。”这就是你想要的吗?请记住, .htaccess 文件中的情况略有不同。

并且:

RewriteRule ^([^/]+)/([^/]?)$ index.php?worker=$1¶ms=$2 [L]

看起来您试图说“将任何“foo/bar”更改为“index.php?worker” =foo¶ms=bar"'.如果这是正确的,那么它可能不会起作用。由于您位于 .htaccess 文件中,因此您将匹配文件系统路径,而不是 URI。

所以试试这个:

RewriteMap  "lc"                 "int:tolower"

RewriteCond "%{REQUEST_URI}"     "[A-Z]"
RewriteRule "(.*)"               "${lc:$1}" [E=REDIRECT:TRUE]

RewriteCond "%{HTTP_HOST}"       "!^www\."  [NC]
RewriteRule "^"                  "-"         [E=REDIRECT:TRUE,E=PREFIX_WWW:TRUE]

RewriteCond "${ENV:REDIRECT}"    "!TRUE"
RewriteRule "^"                  "-"        [SKIP=2]
RewriteCond "${ENV:PREFIX_WWW}"  "TRUE"
RewriteRule "(.*)"               "http://www.${HTTP_HOST}/$1"     [R=301,L]
RewriteRule "^"                  "-"                               [R=301,L]

RewriteRule "^([^/]+)/([^/]?)$"  "index.php?worker=$1¶ms=$2" [PT]

我不确定 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:

RewriteMap  lc int:tolower
RewriteCond "%{REQUEST_URI}" "[A-Z]"
RewriteRule "(.*)"           "${lc:$1}" [R=301,L]

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:

RewriteMap  lc int:tolower
RewriteCond "%{REQUEST_URI}" "[A-Z]"
RewriteRule "(.*)"           "${lc:$1}" [E=REDIRECT:TRUE]

RewriteCond "%{HTTP_HOST}"   "!^www\."  [NC]
RewriteRule "^"              "-"        [E=REDIRECT:TRUE,E=PREFIX_WWW:TRUE]

What is the following trying to do?

RewriteRule ([\w]*)/$ http://%{HTTP_HOST}/$1 [R=301,L]

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:

RewriteRule ^([^/]+)/([^/]?)$ index.php?worker=$1¶ms=$2 [L]

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:

RewriteMap  "lc"                 "int:tolower"

RewriteCond "%{REQUEST_URI}"     "[A-Z]"
RewriteRule "(.*)"               "${lc:$1}" [E=REDIRECT:TRUE]

RewriteCond "%{HTTP_HOST}"       "!^www\."  [NC]
RewriteRule "^"                  "-"         [E=REDIRECT:TRUE,E=PREFIX_WWW:TRUE]

RewriteCond "${ENV:REDIRECT}"    "!TRUE"
RewriteRule "^"                  "-"        [SKIP=2]
RewriteCond "${ENV:PREFIX_WWW}"  "TRUE"
RewriteRule "(.*)"               "http://www.${HTTP_HOST}/$1"     [R=301,L]
RewriteRule "^"                  "-"                               [R=301,L]

RewriteRule "^([^/]+)/([^/]?)$"  "index.php?worker=$1¶ms=$2" [PT]

I'm not sure about the effect of SKIP on RewriteCond statements mixed with RewriteRules, but give the above a try and see if you're any closer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文