.htaccess:非 www 到 www +无扩展名网址 +无索引

发布于 2024-09-19 20:53:31 字数 1524 浏览 4 评论 0原文

我本以为这会在某个地方得到更好的记录,但找不到有关该主题的太多信息。

基本上,我使用 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/

这是我从各种来源编译的脚本,它确实有效,但是当我指向站点地图中的无扩展名网址时,谷歌似乎没有抓取该网站,知道为什么吗?提前致谢。

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:

  1. Redirect / rewrite non-www to www
  2. 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...
  3. 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 技术交流群。

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

发布评论

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

评论(1

じее 2024-09-26 20:53:31

尝试这些规则:

# 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]

Try these rules:

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