使用.htaccess删除重复的链接

时间:2011-06-26 10:14:47

标签: php .htaccess

我知道有一些答案重申这个话题,但我需要更多的东西:)

  • 我需要所有的www页面重定向 到非www
  • 我需要删除所有index.php
  • 我需要从所有人中删除.php 页面(site.com/sss.php到 site.com/sss)
  • 是否有区别 http://site.com/sss/http://site.com/sss(斜线)?

1 个答案:

答案 0 :(得分:2)

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /

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

    # 2. index.php to /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
    RewriteCond %{THE_REQUEST} !^POST
    RewriteRule ^index\.php$ http://example.com/ [R=301,L]

    # 3. remove trailing slashes if it's not a directory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{HTTP_HOST} !^\example\.com$ [NC]
    RewriteRule ^(.+)/$ http://example.com/$1 [R=301,L]

    # 3. remove .php extension from files
    RewriteCond %{THE_REQUEST} ^GET\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://example.com/$1 [R=301,L]

    # 3. internally rewrite to .php if a file exists
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule (.+) /$1.php [L]
</IfModule>

4.是的,添加的斜杠意味着资源是一个目录,而缺少斜杠意味着资源是没有扩展名的文件。

相关问题