Forum Moderators: phranque

Message Too Old, No Replies

.htaccess match base URL without subfolder or querystring

.htaccess match base URL without subfolder or querystring

         

katsmith

12:06 pm on Jan 20, 2011 (gmt 0)

10+ Year Member



I have a problem which seems like it should be simple to answer. When someone goes to the root of my site:

http://www.mynewsite.com/
http://www.mynewsite.com
http://mynewsite.com/
http://mynewsite.com


In these instances, I want to redirect to a particular page:

http://www.mynewsite.com/mynewpage


I can't for the life of me work out the right rewrite conditions to match when someone is viewing the base URL without any additional subfolders, files or querystrings. It seems like it should be simple?!

Thanks in advance for any help.

[edited by: tedster at 3:01 pm (utc) on Jan 25, 2011]
[edit reason] fix a technical problem [/edit]

g1smd

7:01 pm on Jan 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The pattern is
^$
or
!.
as the path to match with the pattern will be "empty".

However, it is bad form to redirect the root of a site to a deep internal URL.

Maybe you need a rewrite instead of a redirect? A rewrite will allow the user to see the content but returned directly at the originally requested URL.

katsmith

9:55 am on Jan 21, 2011 (gmt 0)

10+ Year Member



That works a treat! This is what I ended up with:


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

RewriteCond %{HTTP_HOST} ^www\.mynewsite\.com$ [NC]
RewriteRule ^$ mynewpage/


Thanks very much for you help.

[edited by: tedster at 3:01 pm (utc) on Jan 25, 2011]

g1smd

7:17 pm on Jan 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Add the [L] flag to the last rule.

katsmith

9:47 am on Jan 24, 2011 (gmt 0)

10+ Year Member



Thanks :)

jdMorgan

12:39 am on Jan 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can also delete the RewriteCond on the second rule; If the requested hostname was not initially "www.mynewsite.com", the first rule would have already redirected it so that it would be so...

Therefore, the second rule can't be reached unless the hostname is already correct. Therefore, the rewritecond checking the hostname is superfluous.

Jim