View Full Version : Rewrite url to take contents from a specific direcory?

01-22-2008, 02:37 PM

I've just modified the html layout of a website and I would like to keep all the new files in a directory to keep the previous contents also

So I just created a directory named 22-Jan-2008 and put all files in to it.
Now how can I write a RewriteRule to address this directory when someone tries www.example.com
Something like

RewriteRule %{HTTP_HOST}$ %{HTTP_HOST}/22-Jan-2008/ [L] is possible?

01-22-2008, 06:33 PM
RewriteCond %{HTTP_HOST} ^www.example.com/.*
RewriteRule ^/(.*)$ /22-Jan-2008/$1 [L]

01-23-2008, 04:22 PM
Thanks shyam, but working partially.

I already have an htaccess in the root with
Rule 1
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www.example.com/.*
RewriteRule ^/(.*)$ /22-Jan-2008/$1 [L] doesn't work as such. So I used just
Rule 2
RewriteRule ^(.*)$ 22-Jan-2008/$1 [L] and now it looks for contents in the subfolder rather than root as desired.
My subfolder contains another htaccess to style the url like
Rule 3
RewriteRule ^([^/\.]+)$ index.php?page=$1 [L]

Now the problems are
1)Users can access the site through www.example.com/22-Jan-2007. Can I prevent this?
2)www is not get appended when someone types http://example.com/22-Jan-2007. Can I modify my above Rule 1 to inherit the effect to here also?
3)My index page looks for the url to decide which content to be displayed using

; (which use Rule 3)
where I have a switch($page) and in the default case I'd like to display a custom 404 message.
But this doesn't work if someone type www.example.com/22-Jan-2007/blah. Any solutions?

Sorry If my explanation is too complex. Hope everything is due to some shortage in the rules. I've gone through many articles but didn't get enough grip. Please help.

01-24-2008, 10:59 AM
Finally, I added

ErrorDocument 404 http://www.example.com/error
ErrorDocument 403 http://www.example/error in to my root's htaccess and now all the unwanted urls are nicely catched by the switch($page) block.

Now the wired problems are when I type www.example.com/22-Jan-2008, it displays the home page, means, user may get the idea that there is such a directory.
How can I solve this? (Not a big problem, but for self satisfaction :))

01-30-2008, 08:46 AM
Finally, is there any rule to effectively work

RewriteRule Pattern Substitution, that there should not be a directory having the name Pattern ?