.htaccess:非 www 到 www +无扩展名网址 +无索引
我本以为这会在某个地方得到更好的记录,但找不到有关该主题的太多信息。
基本上,我使用 htaccess 在我正在处理的网站上灌输 3 条规则:
- 将非 www 重定向/重写为 www
- 从每个网站页面中删除扩展名 - 它们是 php 文件。这样做意味着站点索引变为 www.example.co.uk/index 而不是 www.example.co.uk/index.php,因此...
- 将 www.example.co.uk/index 重定向/重写为 www .example.co.uk/
这是我从各种来源编译的脚本,它确实有效,但是当我指向站点地图中的无扩展名网址时,谷歌似乎没有抓取该网站,知道为什么吗?提前致谢。
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]
I would have thought this would be better documented somewhere, but cannot find much information on the subject.
Basically I'm using htaccess to instill 3 rules on the site i'm working on:
- Redirect / rewrite non-www to www
- Remove the extensions from each of the site pages - they're php files. Doing this means that the site index becomes www.example.co.uk/index instead of www.example.co.uk/index.php, so...
- Redirect / rewrite the www.example.co.uk/index to www.example.co.uk/
This is the script I've compiled from various sources, it does work, but google doesn't seem to be crawling the site when i point to the extensionless urls in the sitemap, any idea why? Thanks in advance.
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这些规则:
Try these rules: