views:

84

answers:

6

Hello,

I am looking to permanently redirect all the pages on a domain to one page on the SAME domain, using htaccess/mod_rewrite.

Basically I want any page requested for the domain to return a holding page [ which is index.php] at domain.com/

most of my attempts so far are causing errors as they are throwing the server intoa loop.

Thanks in advance

.k

+2  A: 

You need to exclude the destination you are redirecting to like this:

RewriteEngine on
RewriteRule !^index\.php$ /index.php [L,R=301]

If you just want to redirect requests that can not be mapped to existing files in the filesystem, add this condition:

RewriteCond %{REQUST_FILENAME} !-f
RewriteRule !^index\.php$ /index.php [L,R=301]

But you should rather respond with a 404 in that case.

Gumbo
thanks for that Gumbo,but that redirect everything [css and image] I just want to redirect the webpages
Keet
@Keet: And “the webpages” are what?
Gumbo
What I meant was if a webpage /some/old/page.html was requested, index.php would be returned. but that page require other files ie images/js/css etc. in this case i don't want index.php to be returned !
Keet
@Keet: And how can those URLs be identified?
Gumbo
+1  A: 

Here's a simple implementation:

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php

And a bit more sophisticated one:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Both were taken from Zend Framework Zend_Controller Programmer's Reference Guide. Here's another useful doc Apache Module mod_rewrite.

Ivan Karpan
Thanks Ivan, that first one seems to work, but it seem to display the new page with original page name ? just one question do you know how to change this to a 301 that redirects to the new page, i tried adding [L,R=301] to the end of the line but it breaks it .
Keet
Frankly speaking, Keet, I don't really get what are you trying to accomplish with forcing the actual redirect. Adding `R=301` flag means telling the your Apache server to ALWAYS force redirect all the requests to `index.php`, which BTW is a request itself and gets redirected again and again again. Browser simply ends up in an indefinite loop, which of course totally breaks the whole thing.
Ivan Karpan
A: 

ISR that this was asked here recently - but I can't find the q right now.

Why not just set up the 404 errorhandler to point to the holding page (and keep all other content out of the doc root)

C.

symcbean
@symcbean surely returning 404 is not the best in this situation [SEO?]All the old pages have pagerankings. by returning 301's these should be changes to the new page afaik
Keet
A: 

Thanks to all,

got it working, here it is it might be of help to others:

RewriteRule !^(index.php)|.(js|ico|gif|jpg|png|css|mp3|xml|swf)$ /index.php [L,R=301]

.k

Keet
A: 

Would this be the same for a html index? as all my folders are being indexed

Thanks Rory

rory Hotso
A: 

Hi guys, can anyone of you tell me whatever should I use so that I can redirect all of my pages (entire website) from root/folder1/* to root/folder1/folder2/*

Also along with this I would like to redirect all the pages from root/folder3/* to root/folder1/folder2/*

please give the code(s) to be written in .htaccess and also also where to put the .htaccess file(s).

Also, there root/* is not accessible by me; i have only access to root/folder1/* and root/folder3/*

Thanks in advance..

Black Panther