I'm trying to redirect all .html files in my root directory to a new directory with the exception of index.html I moved all html files in the root to a new directory with the name /s/ except the index.html I don't want to redirect files in subfolders. I can't find out how to resolve this problem with .htaccess. I tried many things but created only internal server errors.
RewriteEngine On RewriteBase / RewriteRule ^index\.html$ index.thtml RewriteRule ^([^/]+)\.html$ s/$1.html RewriteRule ^index\.thtml$ index.html Code (markup):
I've attached a zipfile that contains a directory named "rewrite". Inside rewrite there is Directory "s" Directory "a" .htaccess index.html test.html Inside both "/rewrite/s" and "/rewrite/a" are an "index.html" and "test.html". Each of the *.html files contain their path from the rewrite directory, so "/a/test.html" contains the text "a/test.html". If you place rewrite in the sites root, so you have http://site.com/rewrite/ then you should see the following. site.com/rewrite/index.html -- "index.html" site.com/rewrite/test.html -- "s/test.html" site.com/rewrite/s/ -- "s/index.html" site.com/rewrite/s/test.html -- "s/test.html" site.com/rewrite/a/ -- "a/index.html" site.com/rewrite/a/test.html -- "a/test.html" If this was to be in the site root instead of contained within the rewrite directory, then you would change the value of "RewriteBase" in ".htaccess" to "/" instead of "/rewrite". In case you're wondering, I'm using Apache2-Debian. <Directory /var/www/> Options Indexes FollowSymLinks MultiViews </Directory> Code (markup):
Yes! Thank you very much! Would it be possible to change as well the directory in the url if a file triggers "RewriteRule ^([^/]+)\.html$ s/$1.html" p.e. if I put in the address bar: http://site.com/rewrite/test.html this file will be loaden: http://site.com/rewrite/s/test.html but in the address bar is still this url: http://site.com/rewrite/test.html Is it possible to let the users see the new url?
The RewriteRule can have a redirect flag set to let the browser know it should use the new address instead. It's as simple as adding [R=301] to the end of the RewriteRule RewriteRule ^([^/]+)\.html$ s/$1.html [R=301] Code (markup): This results in the request for "/test.html" returning a 301 status and telling the browser to go to "/s/test.html" from now on instead. With the way it was in my previous example, the browser never knew the swap existed. Basicly doing a "you say tomAto I say tomato".
I added [R=301] to: RewriteRule ^([^/]+)\.html$ s/$1.html New rule: RewriteRule ^([^/]+)\.html$ s/$1.html [R=301] However my domain is a virtual domain and when I add [R=301] the real domain is showing up in the url bar with an error: The requested URL /rewrite/s/test.html was not found on this server.
In that case, you'll need to use the entire url in the RewriteRule with the 301 attached. RewriteRule ^([^/]+)\.html$ http://domain.com/s/$1.html [R=301] Code (markup):