301 redirect for individual pages .htaccess - apache

I have tried various ways to write the .htaccess file but all i get is a 500 server error.
I am trying to redirect all the pages from one website to another. This is because the website has been rewritten and has had a change of name. I want to keep all the seo intact.
I have put the .htaccess file in the root directory and here is a copy
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^yachting-school.co.uk [NC]
RewriteRule ^(.*)$ http://www.yachting-school.co.uk/$1 [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ http://www.dreamortwosailing.co.uk/$1 [R=301,L]
#
Redirect 301 /index.htm http://www.dreamortwosailing.co.uk/index.php
Redirect 301 /essential_stuff/about_us.php http://www.dreamortwosailing.co.uk/info/us/

I think your syntax is slightly out on the rules.
Redirect 301 ^/index.htm$ http://www.dreamortwosailing.co.uk/index.php
Redirect 301 ^/essential_stuff/about_us.php$ http://www.dreamortwosailing.co.uk/info/us/

Related

Redirecting subdomain to subdirectory through htaccess

I've been trying to redirect a subdomain to a subdirectory through htacess..
I have a domain lets say sub.domain.com and the directory domain.com/site/
I want to redirect sub.domain.com to domain.com/site not changing any url, simply redirecting in a SEO friendly way.
I've tried a redirect 301 rule but it doesn't seem to have worked.
Try this in your .htaccess:
RedirectMatch 301 wiki.comp.tf(.*) comp.tf/wiki$1
If that does not work, an alternative option is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^wiki.comp.tf
RewriteRule ^(.*)$ http://comp.tf/wiki$1 [L,NC,QSA]
Some potential options for you using actual names:
Redirect 301 wiki.comp.tf comp.tf/wiki
You can use the following rule :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ /site/$1 [L]
This will internally redirect
sub.domain.com
to
/site/

301 Redirect for specific url

I want to edit the URL of a page (only one page called meto.php) to a custom URL.
It is important that the meto.php file located in sub-directory /pob/
i want to change this www.domain/pob/meto.php to www.domain/pob/define.php
When visitor type this address www.domain/pob/meto.php address bar show this address: www.domain/pob/define.php
Please note define.php file does not exist.
I tried this code in .htaccess file at /pob/ (not root directory):
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com[nc]
RewriteRule ^(.*)$ http://www.domain.com/pob/$1 [r=301,nc]
Redirect 301 meto.php define.php
//also
Redirect 301 /meto.php http://www.domain.com/pob/define.php
//
RewriteRule ^meto.php define.php
Unfortunately all of the above tries fails.
Server info:
Apache Version 2.2
PHP: 5.5
You can not rewrite your files using Redirect Directiv of mod_alias , it is used for external redirection.
Try mod_rewrite
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/pob/$1 [R=301,L]
#1) redirect "/pob/meto.php to "/pob/define.php"
RewriteCond %{THE_REQUEST} /meto.php [NC]
RewriteRule ^ /define.php [NC,L,R]
#2) rewrite "pob/define.php" to "/pob/meto.php"
RewriteRule ^define\.php$ /pob/meto.php [NC,L]

301 Redirect using .htaccess does not work

Here is my rule in .htaccess file:
Redirect 301 /George-Nelson-Bench-CT3005-EDI6.htm?categoryId=-1 http://www.mydomain.com/proddetail.php?prod=George_Nelson_Bench
But this is showing a 404 error on my website.
Some other code on the .htaccess file is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
Some other 301 redirection which works properly:
Redirect 301 /Modern-Classics_c8.htm http://www.mydomain.com/categories.php?cat=10
Redirect 301 /Sofas_c34.htm http://www.mydomain.com/products.php?cat=25
Redirect 301 /Bedroom_c2.htm http://www.mydomain.com/categories.php?cat=7
So, why the first 301 redirect rule is not working?Any suggestions?
Since you're anyway using mod_rewrite its better to replace mod_alias based code to mod_rewrite which is more powerful and flexible.
You first Redirect rule isn't working because you're using a query parameter. Replace that rule with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+George-Nelson-Bench-CT3005-EDI6\.htm\?categoryId=-1\s [NC]
RewriteRule ^ /proddetail.php?prod=George_Nelson_Bench? [R=301,L]

Domain .htaccess redirect issue

I'm trying to create some redirects with .htaccess but I never manage to get it fully functional. Maybe someone here can help me.
What I need is:
http://domain.se and http://domain.com to redirect to http://www.domain.com.
I also need http://domain.se/somefolder, http://domain.com/somefolder as well as http://www.domain.se/somefolder to redirect to http://www.domain.com/folder.
I've tried to accomplish this myself but all I end up with is errors about data not being sent.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# folder rewrite
RewriteRule ^somefolder$ folder [L]
# domain redirect
RewriteCond %{HTTP_HOST} =domain.com [OR]
RewriteCond %{HTTP_HOST} =domain.se
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
This is to be placed in .htaccess file in website root folder. If placed elsewhere some tweaking may be required.
First rule will rewrite (internal redirect) requests to /somefolder to /folder. If you need this to be 301 Permanent Redirect, then replace [L] by [R=301,L]
Second rule will do domain redirect job. This rule will ONLY redirect if domain is domain.com or domain.se. If you want to have redirect from ANY domain name (that your webserver is configured can serve) to www.domain.com then replace those 2 RewriteCond lines with this one: RewriteCond %{HTTP_HOST} !=www.domain.com.
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
That should meet all of your requirements. All requests that are not www.domain.com will be redirected to that domain, with the request URI intact.

How to redirect non-www to www URL's using htaccess?

I have a website say http://www.example.com/ in the root of my website, I have added .htaccess file to redirect any request of http://example.com/ to http://www.example.com/
Recently I have created a new section "Videos" so the URL for videos is http://www.example.com/videos/ . In this video folder I have another htaccess file which is performing rewriting for video entries. When I am trying to access http://example.com/videos/ then its not redirecting me to http://www.example.com/videos/
I think .htacces is not inheriting the previous rules from the parent directory. Can anyone please tell me what can be the rule I can add in the .htaccess file of /videos/ folder so that any request for http://example.com/videos/ will be redirected to http://www.example.com/videos/ URL.
This is a more generic solution, because it can be used with any domain name without having to specify the specific domain name in each .htaccess:
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The contrary is also possible (www to non-www):
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I think it may just be that your existing rule is too strict, and is not getting triggered in your subdirectory because of this. Something like this should work site-wide:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
This is Best Solutions to redirect non-www to www URL's using htaccess. using this code in htaccess file and check url.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
I recommend everyone to stick with the below method it will only redirect the main domain only not any other sub-directory or sub-domain in your host.
# Redirect non-www to www only for main domain
# recommended if you have any/some sub-domain(s)
RewriteEngine on
RewriteBase /
# Replace yoursite and .tld respectively
RewriteCond %{HTTP_HOST} ^yoursite\.tld$
# Replace yoursite.com
RewriteRule ^(.*) http://www.yoursite.com/$1 [R=301]
Use this method if you are really sure and I hope you will that you won't ever use sub-domain with your website i.e. subdomain.yoursite.com or whatever. Than you're welcome to use the below method.
"Saying Again make sure if you really want to use this method"
# Redirect all non-www to www including subdomain(s)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The below method will redirect all www url to non-www including your sub-domains although you cannot use www.subdirecty.yoursite.com it will prompt you an error with 500.
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]