URL Canonicalization - Apache mod_rewrite

时间:2012-02-15 23:02:20

标签: apache codeigniter mod-rewrite url-rewriting normalization

我可能试图将URL Canonicalization稍微过一点,但无论如何它仍然存在。

基本上我期待以下内容:

  1. 301将每个基本网址重定向到http://www.mydomain.com/某些网页是https它应该识别并继续使用https已经使用/请求的地方
  2. 301重定向任何尾部斜杠,即http://www.mydomain.com/page/变为http://www.mydomain.com/page(我已经有一行代码找到index.php页面 - 这个站点建立在Codeigniter上)
    • 我不希望基本网址剥离斜线,这是唯一一次留下斜线
  3. 查找index.php的任何实例(位于网址的正中间或末尾),301将其重定向
  4. 301将我的IP地址的任何使用重定向到实际域
  5. 以下是我目前在根目录中的htaccess文件中所拥有的内容:

    RewriteEngine on
    
    Options +FollowSymLinks 
    RewriteCond %{REQUEST_URI}   ^(.*)(/index\.php)$
    RewriteRule ^(.*)index\.php/$  http://www.mydomain.com/$1 [R=301,L]
    
    RewriteCond %{HTTP_HOST}   ^(mydomain\.com)(:80)? [NC]
    RewriteRule ^(.*)$  http://www.mydomain.com/$1 [R=301,L]
    
    RewriteCond %{HTTP_HOST}   ^11\.11\.111\.111$
    RewriteRule ^(.*)$  http://www.mydomain.com/$1 [R=301,L]
    
    #This makes sure that the server actually finds the index file although its not in the url
    RewriteCond $1   !^(index\.php|images|assets|downloads|css|js|robots\.txt)
    RewriteRule ^(.*)$  /index\.php/$1 [L]
    

    我现在被困住了,任何帮助都会非常感激!!

    修订!!

    我已经取得了一些进展,这是我迄今为止的目标

    <IfModule mod_rewrite.c>
    RewriteEngine on
    
    # index.php to /
    RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
    RewriteRule (.*)index\.(php|html)$ /$1 [r=301,L]
    
    # index.php to / at the base url
    RewriteCond %{THE_REQUEST} ^GET\ /index\.(php|html)\ HTTP
    RewriteRule (.*)index\.(php|html)$ /$1 [r=301,L]
    
    # force www.
    rewritecond %{HTTP_HOST} ^paolienvelope.com [nc]
    rewriterule ^(.*)$ http://www.paolienvelope.com/$1 [r=301,L]
    
    # force no IP
    RewriteCond %{HTTP_HOST} ^70.40.204.154
    RewriteRule ^(.*) http://www.paolienvelope.com/$1 [r=301,L]
    
    #codeigniter direct
    RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    </IfModule>
    

    这成功强制ip到url 从url中删除index.php或index.html,但正确指向索引文件 确保基本网址有www。 仍然没有代码只从请求中删除尾部斜杠 任何帮助,将不胜感激!!谢谢!

2 个答案:

答案 0 :(得分:1)

RewriteEngine on

# index.php remove any index.php parts
RewriteCond %{THE_REQUEST} /index\.(php|html)
RewriteRule (.*)index\.(php|html)(.*)$ $1$3 [R=301,L]

# force www. (also does the IP thing)
RewriteCond %{HTTP_HOST} !^www\.paolienvelope\.com [NC]
RewriteRule ^(.*)$ http://www.paolienvelope.com/$1 [R=301,L]

# remove tailing slash
DirectorySlash off
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js)
RewriteRule ^(.*)/$ $1 [R=301,L]

# codeigniter direct
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

答案 1 :(得分:0)

让我们尝试解决这一点。如上所述我还没有专业人士,但我猜有点帮助:

我将从 2开始。,因为那个似乎更容易:

#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.\.com$ [NC]
RewriteRule ^(.+)/$ http://www.mydomain.com/$1 [R=301,L] 

这是否有效,而不是你的工作?

源: http://blog.valtersboze.com/2009/06/add-or-remove-trailing-slash-in-url-with-htaccess/