youeye
youeye

Reputation: 737

redirecting all dynamic urls to index page

as i want to redirect all dynamic urls like http://www.mysite.com/play.php?id=4820 to http://www.mysite.com/

i have so many dynamic urls i dont want my user to see the page like page not found etc. so whenever a user try to access the dynamic url like the above he should be redirected to home page. please can anyone tell me how to achieve this using .htaccess

Regards, phphuger.

Upvotes: 2

Views: 279

Answers (3)

anubhava
anubhava

Reputation: 785491

Enable mod_rewrite and put this code in your .htaccess under DOCUMENT_ROOT:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+play\.php[\s\?] [NC]
RewriteRule ^ / [L,R]

Upvotes: 1

Gohn67
Gohn67

Reputation: 10648

EDIT2

Ok, now I think understand your question.

You have url at play.php that used to handle all your urls and you want them to be redirected to the homepage.

Here are two possible solutions. This is the first:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/play.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>

The above will redirect any requests to play.php to your home page using a 301 redirect, which means the redirect is permanent. This is different than a 302 redirect which is temporary. So it is important to redirect using 301.

Also in this rewrite rule, we don't care if the url exists or not. So you don't need to have the actual play.php file or anything. It just matches based on the url.

On the line for RewriteRule, there is a question mark at the end, this is to erase the query string from the url on redirect. I'm assuming you don't want the query string to carry over.

The [NC] is for case insensitive matching, so /PlAy.php would be redirected also

An alternative is use the Redirect directive:

Redirect 301 /play.php http://www.mysite.com/

This will do a 301 permanent redirect to your homepage if the user tries go to play.php. The only downside of this is that the query string shows up. You can add a question mark at the end and it will erase the query string. Unfortunately the question mark stays.

If you happen to have multiple endpoints and not just play.php, you can do the following:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/play\.php* [NC, OR]
RewriteCond %{REQUEST_URI}  ^/another_url\.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>

This RewriteRule will match play.php or another_url.php and do 301 redirect to your home page.

Alternatively:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(play|another_url)\.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>

Here is the alternative using the Redirect directive

Redirect 301 /play.php http://www.mysite.com/
Redirect 301 /another_url.php http://www.mysite.com/

Alternative you could also use the RedirectMatch directive to use regex expressions (Note I haven't actually tested this one)

RedirectMatch 301 /(play|another_url)\.php http://www.mysite.com/

EDIT

Ignore this answer. I misread your question. The following redirects all requests to index.php unless the file or directory exists, which is not what you want now that I read your question.

Here is how Wordpres does it. I believe Zend and Joomla also have variations of this.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Upvotes: 0

adarshr
adarshr

Reputation: 62603

I think this should do.

RewriteRule ^\?.*$    http://www.mysite.com

Upvotes: 0

Related Questions