使用 .htaccess 创建谷歌风格的网址


Creating google style urls with .htaccess

我正在努力使用.htaccess创建Google风格的SEO规则,但我在最后一部分遇到了问题。

如果您在浏览器中键入 google.com/nexus,您将被重定向到 www.google.com/nexus/

它添加了 www 以及尾部斜杠。

如何使用 .htaccess 实现此目的?

#Start rewrite engine
RewriteEngine on
RewriteBase /
# Enforce www
# If you have subdomains, you can add them to 
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|dev|d10|m)'.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Hide the private directory by redirecting the request to index.php
RewriteRule ^(private|'.git) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

如果您看到任何需要改进速度/可扩展性的地方,我们将不胜感激。

我的最终解决方案:

RewriteEngine on
RewriteBase /        
# Enforce www
# If you have subdomains, you can add them to 
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|dev|d10|m)'.    
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Add Trailing Slash
RewriteCond %{REQUEST_URI} !('.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

# Hide the private directory by redirecting the request to index.php
RewriteRule ^(private|'.git) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
RewriteCond %{REQUEST_URI} !('.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://example.com/$1/ [R=301,L]

将 example.com 替换为您的域。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain'.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L] 

用于领导 wwwhttp://www.cyberciti.biz/faq/apache-redirect-domaincom-to-wwwdomaincom/