Forum Moderators: phranque

Message Too Old, No Replies

Rewriting the URL for search engines.

Will this work?

         

Teknorat

1:03 am on Jul 22, 2004 (gmt 0)

10+ Year Member



The idea of this is to remove the numerous subdomains all pointing to the same IP we have and rewrite them to the main domain. (Apart from the exceptions of www and example) is this written correctly. Because we have had no luck gettng it to work as yet.


RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST}!^((www¦example).example.com¦example.com)$
RewriteRule (.*) [example.com...] [R=301,L]

gergoe

1:39 am on Jul 22, 2004 (gmt 0)

10+ Year Member



Try this one:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^((www¦example)\.)?example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]

If you've placed this in a htaccess file in the root directory of all of the domains/subdomains, then it should do the trick. Don't forget to change the broken pipe (¦) character to a solid one before using it, this board has a funny habbit of replacing some characters ;-) If you have this in a httpd.conf, then you don't need the RewriteBase directive and the RewriteRule should look like this:


RewriteRule [b]/[/b](.*) http://example.com/$1 [R=301,L]

Actually there wasn't any crucial problem in your rewrites, except few minor ones, like the . characters must be escaped otherwise it will not work properly, the first RewriteCond you need to put there to prevent the redirection deadloop of a non http/1.1 capable browser (which does not send the HOST header, so you can't check it from your rules), and the regexp pattern was a bit mixed up, could be that it was not working (well, at least I cannot say it for sure, I'm not a regexp interpreter after all ;-)

And finally if you did not have the mod_rewrite enabled, or if you're using these rules in a htaccess file you might need to be sure that the FollowSymLinks option is enabled somewhere, to be on the safe side add it to the top of the rules.

See the mod_rewrite documentation [httpd.apache.org] for details on the different comments.

Teknorat

2:30 am on Jul 22, 2004 (gmt 0)

10+ Year Member



Thanks for that! I'm continuing experimenting.