使用.htaccess创建Google格式的网址

时间:2012-06-01 19:43:55

标签: php .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]

2 个答案:

答案 0 :(得分:1)

RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://example.com/$1/ [R=301,L]

将example.com替换为您的域名。

答案 1 :(得分:0)

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

领导www http://www.cyberciti.biz/faq/apache-redirect-domaincom-to-wwwdomaincom/