Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond newbie help

         

mr_tart

3:16 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



I've not played with htaccess before but someone advised me to do this for the solution needed and I'm 99% there.

I had a forum in my root (public) folder.
I've now moved it to /forum.

I would like a redirect for anyone that has mydomain.co.uk bookmarked to land them at mydomain.co.uk/forum. Except for me, on a fixed ip.

I am trying to install a wordpress blog in the root folder and it's going to take me a few days to get all the content and widgets all nice and tidy so I'd like to do this but for the outside world to be oblivious.

This is what I have so far:

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.89$
RewriteRule ^/$ www.example.co.uk/forum/index.php [R]
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.89$
RewriteRule ^index\.php$ www.example.co.uk/forum/index.php [R]

From what I can tell it is the first rewrite rule that is not working so I can only assume the syntax is not correct.
Can anyone help me out here?

Thanks

Matt

[edited by: jdMorgan at 4:33 pm (utc) on Apr 8, 2010]
[edit reason] example.co.uk [/edit]

jdMorgan

4:29 pm on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"Oblivious" requires that you use an internal rewrite, not an external redirect.

In .htaccess, the path to the .htaccess file's location is stripped from the URL-path examined by RewriteRule. In the case of your 'top-level' .htaccess file, this stripped path is "/". Therefore, your pattern (which includes this slash) will never match.

Addressing both of those problems and improving the code efficiency yields:

RewriteEngine on
#
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule ^(index\.php)?$ /forum/index.php [L]

However, this brings up another issue, in that it should not be necessary to provide for "index.php" requests in this rewrite rule, because you should already have a rule in place to 301-redirect those requests back to "/", along with a hostname canonicalization rule --at a minimum-- in order to prevent duplicate-content problems:

RewriteEngine on
#
# Redirect direct client requests for "/index.php" back to "/"
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^index\.php$ http://www.example.co.uk/ [R=301,L]
#
# Redirect non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} !^(www\.example\.co\.uk)?$
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]
#
# Rewrite root index page requests to /forum subfolder index page
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule ^$ /forum/index.php [L]

Added: Please do feel free to avail yourself of the resources cited in our Apache Forum Charter. Your success with mod_rewrite will be much better by doing so. The link to our charter (and library, etc.) is at the top of this page.

Jim