redirecting all dynamic urls to index page - php

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.

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]

I think this should do.
RewriteRule ^\?.*$ http://www.mysite.com

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

Related

Redirect all domains of .com to .org on a wordpress site

I need a help on a site redirection, i have 2 domains : mywebsite.com and mywebsite.org, they both point to the same site (wordpress), and the .com is already indexed by google, i want to redirect all *.com links to www.mysite.org links also all non www of .org to www.mywebsite.org
I have done like this but nothing works :
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS_HOST} ^(www\.)?mywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.org/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
What i have done wrong here?
thank you
RewriteCond %{HTTPS_HOST} ^(www\.)?mywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.org/$1 [R=301,L]
As noted in comments, HTTPS_HOST does not exist, it should be HTTP_HOST. So, the above condition will simply fail to match and no redirect occurs.
So, if you correct the above variable then this should work to redirect all traffic from mywebsite.com to www.mywebsite.org, maintaining the URL-path.
However, since this is WordPress, you should avoid manually editing the directives that are part of the front-controller, ie. the code between the # BEGIN WordPress and # END WordPress comment markers. Any redirect that you add should go before the # BEGIN WordPress block at the top of the `.htaccess file.
This also doesn't redirect the non-www version of the .org domain.
If you only have these 2 domains then you can simplify the redirect by checking that the requested hostname is not the canonical hostname (ie. is not www.mywebsite.org) and redirecting accordingly.
Try the following instead:
Options +FollowSymlinks
# Canonical redirect
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.org$
RewriteRule (.*) http://www.mywebsite.org/$1 [R=302,L]
# 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
The ! prefix on the CondPattern negates the regex, so the condition is only successful when it does not match.
Test with 302 (temporary) redirects and only change to 301 (permanent) when you are sure this is working OK - to avoid potential caching issues.
Make sure you have cleared your browser cache before testing.
Note that you are redirecting to HTTP (not HTTPS) - is that intentional?

.htaccess-The page isn't redirecting properly

I am trying to redirect index.html to home.html and also remove that home.html from url via .htaccess. But it shows The page isn't redirecting properly.
Which Means i need to redirect mydomain.com/index.html to mydomain.com/home.html and I need to show mydomain.com/ only when I visit index page.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Redirect 301 "/index.html" "/home.html"
RewriteCond %{THE_REQUEST} ^GET.*home\.html [NC]
RewriteRule (.*?)home\.html/*(.*) /$1$2 [R=301,NE,L]
</IfModule>
What you could try instead of utilizing mod_rewrite is setting DirectoryIndex to the desired page (in your case home.html):
DirectoryIndex home.html
So, if you're calling domain.com/ it should give you the contents of domain.com/home.html
For reference, the Apache configuration manual:
https://httpd.apache.org/docs/2.4/mod/mod_dir.html
"DirectoryIndex Directive"
Referencing the conversation right below:
Maybe this could work (Sorry, I cant test it right now)
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} =/index.html
RewriteRule (.*?)home\.html/*(.*) /$1$2 [R=301,NE,L]
A bit of explanation: If the requested host matches example.com and the called URI is in fact not a real file or folder plus the URI equals to
"index.html" it should redirect to home.html.
EDIT: As you dont want to show the home.html we can maybe get away by "proxying" the request internally instead of forcing an external redirect. Try to replace "R=301" in the last line (the RewriteRule) with "P" so it says RewriteRule (.*?)home\.html/*(.*) /$1$2 [P,NE,L]
// It's working code i hope it's useful ..
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect 301 / http://newsite.com/
</IfModule>
or
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?http://dbdenterprise\.in$
RewriteRule ^(.*)$ http://dbdenterprise.com/$1 [R=301,QSA,L]

'Fetch as Google' in Webmaster tool says 'Redirected'

Google webmaster tool shows me this for my website. https://ibb.co/cnCOfk
This is a WP website, and my guess is it's because the redirection from non-www to www is not happening. Google fetches with www and then my website gets redirect too non-www domain. How do I fix this problem?
I found online that I can solve this with .htaccess. Now, I added last 2 lines (above IfModle ends). Currently, my .htaccess looks like 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]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END WordPress
Once I add this, website goes in infinite loop of "www.domainname.co" to "domainname.co" to "www.domainname.co" and so on.
How do I solve this problem? Thank you in advance.
It would seem this redirect is occurring within WordPress itself - that is where you need to fix this. You can then repeat the same redirect in .htaccess if you wish, as that would be more efficient.
But if you do the opposite redirect in .htaccess then you'll naturally get a redirect loop.
EDIT: And if you implement this redirect in .htaccess, it should go before the WordPress front controller.

mod_rewrite and 301 redirect in the same htaccess

I have the following .htaccess file:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*)$ ./page.php?name=$1
I also have about 20 different static addressess I want to do a 301 redirect to.
It should look something like:
Redirect 301 http://www.example.com/category.php?id=3 http://www.example.com/books
Redirect 301 http://www.example.com/articles.php?id=124 http://www.example.com/birds
I tried every method and failed.
Can somebody help me?
I'm guessing your after a set of SEO friendly URL's for those in your redirect examples, if this is the case then you don't want a browser redirect, instead you want an Internal Redirect or Alias e.g.
RewriteRule ^/?books /category.php?id=3 [L]
RewriteRule ^/?birds /articles.php?id=124 [L]
If you have many rules and have access to the httpd.conf you may want to consider a rewrite map file and rule.
Per your comment BELOW, if you want to force users/crawlers to the new URL structure then you'll need an additional set of Rewrite rules in the file (not redirects) e.g.
RewriteCond %{THE_REQUEST} /category\.php [NC]
RewriteCond %{QUERY_STRING} ^id=3 [NC]
RewriteRule ^.* http://%{HTTP_HOST}/books? [R=301,L]
FYI: The Apache mod_rewrite documentation is worth a read if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post.

301 redirects and wordpress pretty permalinks

I am moving from an old php based site to a new one based on wordpress. In the process we are getting rid of quite a few unnecessary pages.
However some of the pages are showing on google, and I want to avoid 403 or 404 errors by using 301 redirects to pages now containing the same info on the new site.
My current .htaccess shows:
# Switch rewrite engine off in case this was installed under HostPay.
RewriteEngine Off
SetEnv DEFAULT_PHP_VERSION 53
DirectoryIndex index.cgi index.php
# 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
and I was wondering what the best way to do 301 redirects is? putting them before or after the code just gives a 404 error.
This should work;
redirect 301 /old/old.htm http://www.website.com/new.htm
Putting a rule similar to this before the index.php rewrite rules should work:
RewriteRule ^(.*)$ http://newwebsite.com/$1 [R=301,L]
You can put a bunch of RewriteCond's before it to catch only those pages with issues.
For the specific case you posted in the comments, this rule works:
RewriteRule ^why-choose-us.* about-us [R=301,L]

Categories