.htaccess:非www到www + extensionless urls +无索引

时间:2010-09-17 13:12:37

标签: .htaccess mod-rewrite redirect url-rewriting no-www

我原本以为这会在某处更好地记录,但找不到关于这个主题的更多信息。

基本上我正在使用htaccess在我正在处理的网站上灌输3条规则:

  1. 将非www重定向/重写为www
  2. 从每个网站页面中删除扩展程序 - 它们是php文件。这样做意味着网站索引变为www.example.co.uk/index而不是www.example.co.uk/index.php,所以......
  3. 将www.example.co.uk/index重定向/重写为www.example.co.uk/
  4. 这是我从各种来源编译的脚本,它确实有用,但是当我指向站点地图中的无扩展网址时谷歌似乎没有抓取网站,任何想法为什么?提前谢谢。

    Options +FollowSymlinks
    RewriteEngine On
    
    # Rewrite index.php to /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/#?\ ]+/)*index\.php?\ HTTP/
    RewriteCond %{HTTP_HOST} ^(www\.example\.co\.uk) [OR]
    RewriteCond www.%{HTTP_HOST} ^(www\.example\.co\.uk)
    RewriteRule ^(([^/]+/)*)index\.php?$ http://%1/$1 [R=301,L]
    
    # Rewrite example.co.uk to www.example.co.uk for canonic purposes, this rule is paired with the previous
    RewriteCond %{HTTP_HOST} ^example\.co\.uk [NC]
    RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]
    #REMOVE .php from file extensions
    # If the requested URI does not contain a period in the final path-part
    RewriteCond %{REQUEST_URI} !(\.[^./]+)$
    # and if it does not exist as a directory
    RewriteCond %{REQUEST_fileNAME} !-d
    # and if it does not exist as a file
    RewriteCond %{REQUEST_fileNAME} !-f
    # then add .php to get the actual filename
    RewriteRule (.*) /$1.php [L]
    
    # If client request header contains php file extension
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+php\ HTTP
    # externally redirect to extensionless URI
    RewriteRule ^(.+)\.php$ http://www.example.co.uk/$1 [R=301,L]
    

1 个答案:

答案 0 :(得分:0)

请尝试以下规则:

# Redirect / rewrite non-www to www
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteCond %{HTTPS}s://%1 ^on(.+)|offs(.+
RewriteRule ^ http%1%2%{REQUEST_URI} [L,R=301]

# Remove the extensions from each of the site pages - they're php files. Doing this means that the site index becomes www.site.com/index instead of www.site.com/index.php, so...
RewriteCond %{THE_REQUEST} ^GET\ (/[^?\ ]+)\.php[?\ ]
RewriteRule .+\.php$ %1 [L,R=301]

#Redirect / rewrite the www.site.com/index to www.site.com/
RewriteRule ^index$ / [L,R=301]