Redirect URLs using .htaccess
Sometimes you need to redirect some URL and/or page on your site to another one.
The feature is very useful if you have recently redesigned your site but you wish to keep the old addresses working for various reasons (you have links to these pages from other sites, some users may have the old pages bookmarked, etc).
The Apache web server provides several way for setting up redirects.
The most simple one is using the “Redirect” directive:
Redirect /folder http://www.example.com/newfolder
With such a line in your .htaccess if a visitor tries to load http://www.example.com/folder, he will be redirected to http://www.example.com/newfolder.
Recently it has been talked a lot about Permanent redirects. The good news is that you can add a status code to the Redirect directive. For example for Permanent 301 redirect you can use:
Redirect permanent /folder http://www.example.com/newfolder
Another useful directive is the RedirectMatch. With it you can use regular expressions in the redirect condition. For example
RedirectMatch "\.html$" http://www.example.com/index.php
This will redirect all requests to files that end with .html to the index.php file.
There is another more powerful way to create redirects or even create transperant redirects which requires ModRewrite. We will talk about this in the next article.
- How to block users from accessing your site based on their IP address
- How to prevent or allow directory listing?
- How to change the error documents – 404 Page Not Found, etc
- Using .htaccess for password protecting your folders
- Using .htaccess to block referrer spam
- Disable Hot-Linking of images and other files
- Redirect URLs using .htaccess
- Introduction to mod_rewrite and some basic examples
- Force SSL/https using .htaccess and mod_rewrite
- 301 Permanent redirects for parked domain names
- Enable CGI, SSI with .htaccess
- How to add Mime-Types using .htaccess
- Change default directory page
- Block Bad robots, spiders, crawlers and harvesters
- Make PHP to work in your HTML files with .htacess
- Change PHP variables using .htaccess
- HTTP Authentication with PHP running as CGI/SuExec
- Force www vs non-www to avoid duplicate content on Google
- Duplicate content fix index.html vs / (slash only)
Comments 40 >>
How do I redirect all links for www.domain.com to domain.com ?
Description of the problem:
Your website can be accessed with www.domain.com and domain.com. Since Google penalizes this due to duplicated content reasons
Solution
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
The following link will be helpful to have a good idea on redirection using .htaccess.
http://basilvarghese.co.cc/using-htaccess-for-redirection.html
I'm using it to remove all wwws from my url - so now all my pages are only at http://scottjarvis.com
Thanks!
Well and I need rewrite rule for following requirement
my web pages have several links with different query strings ,my case is if click the any link in that page it must redirect in to respective page with defined variable/query string
i hope following example will help you to understand
Link: http://myweb.com/index.php?module=mine&page=login
After click the link it will like
http://myweb.com/index.php?module=mine&page=login&VCODE=777
is this possible it .htaccess please let me know the same, thanks in advance expecting your positive reply
Best regards,
Sujith
how do i redirects old_domain/folder/default.asp to new_domain/index.php
thx
http://www.xyz.com/?a=112 tohttp://www.xyz.com/
Please advise
Thanks in advance
like this.
can you help me to do so?
http://www.mywebsite.com/company/42/3/services-overview/24/services-overview/company/47.html
to ------>
http://www.mywebsite.com/
The command line im using for the .htaccess is redirect 301:http://200.37.59.94/juntosformajosdesarrollo.asp?por=1&en=18 to http://www.acerosarequipa.com/index.php?id=juntosforjamosdesarrollo0
Im having problems what should i do?
The Redirect directive cannot help as it cannot handle the GET params you have.
You will need to use ModRewrite to handle the redirect. For example:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/juntosformajosdesarrollo\.asp$
RewriteCond %{QUERY_STRING} ^por=1&en=18$
RewriteRule ^(.*)$ http://www.acerosarequipa.com/index.php?id=juntosforjamosdesarrollo0 [R=301,L]
I am not sure how your old site params relate to your new site, but basically rules as above should do the trick.
problem. Any help is appreciated! I want to redirect my /index.html
to my "/" (www.sample.com). There is only one domain on the server.
I am using the following script:
#to direct index.html to www.mysite.com
RewriteEngine On
RewriteCond%{THE_REQUEST} /index\.html\ HTTP/
RewriteRule ^index\.html$ /[R=301,L]
It works, but if you type other words in your browser,
it brings up a webpage but without all the shapes/pics/etc. For
example: www.mysite.com/paper/index.html would bring up the webpage
www.mysite.com/paper with the text, but without the shapes, pics,
etc.
I want the .htaccess to redirect those types of requests (with extra words, like adding to index) to the www.sample.com/ page (and of course, without the .index.)
I tried:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\/(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$http://www.example.com/%1 [R=301,L]
It works about the same as the first example.
Bottom line, I want any requests with any extra "words" (e.g., www.mysite.com/words/index.html, etc.)just to be directed to my "index" page.
How i can redirect http://www.example.com/default.asp?resolutionfile=templatespath|default.asp to http://www.example.com/
please give me suggestion
thanks
The RedirectMatch is EXACTLY what I needed!
Great site. Very helpful.
http://www.jamesblakesleedesign.com/contact.aspx
point to:
https://www.jamesblakesleedesign.com/contact.aspx
Can you please tell me how to do this?
Thank you,
Jim
So I think maybe it's htaccess problem.. how to prevent it? please help;
I'm from Brazil. How I can redirect for HTTPS/SSL these rules ?
RewriteRule .* index.php [L]
RewriteCond %{HTTP_REFERER} !^http://example.com.br/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com.br/.*$ [NC]
Thanks.
how to redirect http://mysite.com/index.php to http://mysite.com
http://www.abusinessusa.com/view_profile.php?id=9999&city=miami&county=3816&state=FL.... and my boss asking me that he need that when he typed http://www.abusinessusa.com/business_name is redirected to the business_name profile.
How I can do that with htaccess?
thank you in advance.
http://www.domainname.com/?chat
or
http://www.domainname.com/?referer=www.yahoo.com
both the above to redirect to
http://www.domainname.com
any help quickly will be appreciated
http://www.oldurl.com/ to redirect to
http://www.domainname.com keeping displaying the oldurl.com in the address bar but arriving as if it is an addon domain
advice will be appreciated
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Hi Mick
Please try this:
You can use .htaccess to redirect users to a different URL. The .htaccess looks for any request for a specific page and if it finds that request, it forwards it to a new page you have specified.
The syntax is:
redirect accessed-file URL-to-go-to
There are 3 parts;
(1) the Redirect command,
(2) the location of the file/directory you want redirected, and
(3) the full URL of the location you want that request sent to.
These parts are separated by a single space and should be on one line.
For example, if you want to redirect users from oldfile.html in the www directory of your account, myaccount, to newpage.html, the syntax should be
redirect /~myaccount/oldfile.html http://www.indiana.edu/~myaccount/newpage.html
Anyone going to http://www.indiana.edu/~myaccount/oldfile.html will end up on http://www.indiana.edu/~myaccount/newpage.html.
You must use a full URL even if you're going to send the users to another page on your own site.
You can also redirect an entire directory:
redirect /~myaccount/old_dir/ http://www.indiana.edu/~myaccount/new_dir/
Anyone going to http://www.indiana.edu/~myaccount/old_dir/filename.html will end up on http://www.indiana.edu/~myaccount/new_dir/filename.html.
After you set up the redirect, you should test it by going to the old URL.
Hi, you can use for example:
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/register
RewriteRule ^(.*)$ https://www.your-domain.com/$1 [R=301,L]
please help for redirect :
http://www.mysite.com/index.php?route=information/contact
to:
http://www.mysite.com/contact
The longer you life the more you learn! :)
what is the method ?
I created a new subdomain (cp.domain.com) that pointing to a remote server. now I want to redirect domain.com/cp and all of its contents to the subdomain. (Both have same contents but on different servers)
e.g. if someone try to open domain.com/cp/signup.php it redirect to same page on subdomain (cp.domain.com/signup.php) automatically.
Is this possible?
May someone help me to figure this out ?
Happy New Year
| Previous: Disable Hot-Linking of images and other files | Next: Introduction to mod_rewrite and some basic examples |
