how to remove parts of a url with htaccess
My url contains a 开发者_Python百科broken link due to the http:// prefix not being added in places. How would I replace this using mod_rewrite:
http://website.com/www.websitelink.com
should go here:
http://www.websitelink.com
RewriteRule ^www\.websitelink\.com$ http://www.websitelink.com/ [R=301,NC,L]
In other words, if your path is /www.websitelink.com (^
is start of string,$
is end of string; in regular expressions, dots are one-character wildcards and have to be escaped)
(and [NC]
matching is not case sensitive - /WwW.webSiteLink.COM would match, too),
[R=301]
redirect with status "301 (Moved Permanently)"
to http://www.websitelink.com/
and [L]
leave processing (no more rewrite rules are processed).
Note that this will work regardless of the site's domain (would work e.g. for http://website.com/www.websitelink.com and http://www.website.com/www.websitelink.com )
If you want to match all the paths that end with your domain, drop the starting ^
:
RewriteRule www\.websitelink\.com$ http://www.websitelink.com/ [R=301,NC,L]
and if you want to match even paths without www.
, make it optional:
RewriteRule (www\.)?websitelink\.com$ http://www.websitelink.com/ [R=301,NC,L]
As @Litso noted, this won't match the path after the "domain-in-path"; this should match the trailing path:
RewriteRule (www\.)?websitelink\.com/(.*)$ http://www.websitelink.com/$1 [R=301,NC,L]
To match any subdomain:
RewriteRule ([a-z0-9.-]+\.)?websitelink\.com/(.*)$ http://www.websitelink.com/$1 [R=301,NC,L]
And to match any domain:
RewriteRule ([a-z0-9.-]+\.)?([a-z0-9.-]+)\.com/(.*)$ http://www.$1.com/$2 [R=301,NC,L]
精彩评论