homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Redirect Question
.htaccess 301

 11:23 pm on Aug 1, 2003 (gmt 0)

I've found more than one remote link using "ww" or "wwww" insead of "www"
I'd like to write one rule to deal with this. Would something like this work to effectively deal will these types of mistakes?

RedirectMatch 301 ^my_domain [my_domain.com...]




 3:48 pm on Aug 2, 2003 (gmt 0)

I think what you have will only work for the site root. I'm no regex wiz but I think something like:

RedirectMatch 301 ^my_domain(.*)$ [my_domain.com...]

will correct links to the site's inner pages as well.


 6:04 pm on Aug 2, 2003 (gmt 0)


The various Redirect directives all require a local path on the left side, and so are not aware of the requested domain. I'd recommend using mod_rewrite to solve this problem.

As long as these requests actually arrive at your server, we can probably use the ever-popular "not the right subdomain" rewrite to do this. However, if the "wwww." and "ww." requests are not mapped to your server by DNS, then we can't do anything about it in .htaccess; this would have to be handled in your DNS set-up first (using "wild-card" DNS).

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com
ReewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

However, if you have other defined subdomains that you use, the above code will make them inaccessible. In that case, you'll need to handle the incorrect subdomains either discretely or by counting the w's:

RewriteCond %{HTTP_HOST} ^(ww¦wwww)\.mydomain\.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(w{1,2}¦w{4,})\.mydomain\.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

As usual, you'll need to replace the broken vertical pipe "¦" characters above with solid vertical pipes.


 6:13 pm on Aug 2, 2003 (gmt 0)


"Syntax: RedirectMatch [status] regex URL" and "Syntax: ScriptAliasMatch regex file-path¦directory-path"

Most mod_alias directives use a local path as the first parameter, these don't. I'm uncertain as to the regex pattern I suggested though.


 6:28 pm on Aug 2, 2003 (gmt 0)

As long as these requests actually arrive at your server, we can probably use the ever-popular "not the right subdomain" rewrite to do this.

Thanks Dave, Jim. Yes, the requests are answered by my server and the pages are displayed regardless, however with my anti-image-leeching code, the pages fall victim to my own safeguards. Rather than continue to add numerous mispelled domains to my rewrite, I'm seeking to do it with one rule. I use no subdomains, so I'm understanding you to say that this should work to change the typos to the correct URL:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
ReewriteRule ^(.*)$ [mydomain.com...] [R=301,L]


That didn't work - it shut down the site, producing a "processing error" page from the server. I had changed the "ReewriteRule" to "RewriteRule" assuming the extra "e" was itself a typo.


This was the error in the logs:

[Sat Aug 2 12:21:07 2003] [alert] [client] /www/m/my_account/htdocs/.htaccess: RewriteCond: bad argument line '%{HTTP_HOST}!^www\.my_domain\.com'


 8:49 pm on Aug 2, 2003 (gmt 0)

Ack! - Typo! Yes, I accidentally put an extra "e" in Rewrite, and you must have a space between the "}" and the "!" in the RewriteCond.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

The RedirectMatch doc says it matches the supplied regex against the requested URL-path, so I assumed that this was a local path specification, just like that used in Redirect, but in regex format. I've never tried to use a full URL in RedirectMatch myself - does it work?



 10:25 pm on Aug 2, 2003 (gmt 0)

Thanks Jim, that did the trick! (the needed space fixed it)


 10:26 pm on Aug 2, 2003 (gmt 0)

Frankly, I don't know Jim. The docs [httpd.apache.org] I'm looking at use this as an example:
RedirectMatch (.*)\.gif$ [anotherserver.com$1.jpg...]
so I assume it does.

I do recall a LONG time back, when the board was much smaller and, collectively, we were not so experienced, that RedirectMatch was commonly used and suggested to members.


 10:39 pm on Aug 2, 2003 (gmt 0)


Yes, no doubt you can place a full URL on the right side as a "target" URL and redirect to a different domain based on matching all or part of the REQUEST_URI, but the question is whether a hostname can appear in the pattern on the left side and be tested against the current HTTP_HOST variable in order to enable or disable the redirect taking place.

Now I'm gonna have to root around and see if I still have access to a site where I have not already implemented alternate-domain-name redirection so that I can test this! Can't stand not knowing... :)



 10:48 pm on Aug 2, 2003 (gmt 0)

Can't stand not knowing...

<confession>I was counting on that! :)</confession>

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved