I am trying to create multiple rewrite rules, so that a few pages will be redirected to certain pages, and the rest will be redirected to the start page. However, all my pages keep getting redirected to the start page.
This is the code I am using:
RewriteCond %{HTTP_HOST} ^site\.com/category\.php?s=1$ [NC]
RewriteRule (.*) http://site.co.uk/category/? [R=301,L]
RewriteCond %{HTTP_HOST} ^site\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.site\.com$ [NC]
RewriteRule (.*) http://site.co.uk/? [R=301,L]
Edit:
This is the full .htaccess:
Order deny,allow
DirectoryIndex default.php index.php
SetEnv DEFAULT_PHP_VERSION 5
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /flavours\.php?\s=1 [NC]
RewriteRule ^ http://site.co.uk/flavours/? [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?site\.com$ [NC]
RewriteRule ^ http://site.co.uk/? [R=301,L]
This is the link I am trying to access: www.site.com/flavours.php?s=1
HTTP_HOST cannot match REQUES_URI.
You can use:
# specific redirects
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /flavours\.php\?s=1 [NC]
RewriteRule ^ http://site.co.uk/flavours/? [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?flaverco\.com$ [NC]
RewriteRule ^ http://site.co.uk/? [R=301,L]
Make sure to clear your browser cache before testing this.
Related
Im trying to redirect several urls with 3 parameters to different static urls with .htaccess but nothing working.
1.
http://olddomain.com/index.php?id_category=28&controller=category&id_lang=2
to
https://newdomain.com/page1/
http://olddomain.com/index.php?id_category=30&controller=category&id_lang=2
to
https://newdomain.com/page2/
http://olddomain.com/index.php
to
https://newdomain.com
I tried the below code but http://olddomain.com/index.php not going to https://newdomain.com :
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page2/? [R=301,L]
You need to have specific longer matches first and then have rules to remove index.php or domain redirect:
RewriteEngine On
# specific redirects with index.php as optional match
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page1/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page2/? [R=301,L,NC]
# remove index.php and redirect to newdmain
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^(?:index\.php/?)?(.*)$ https://newdomain.com/$1 [L,R=301,NC,NE]
Make sure to clear your browser cache before testing this change.
In case you are taking page's id from id_lang= variable then please try following rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules to redirect to link: https://newdomain.com/page1/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\?id_category=28&controller=category&id_lang=(\d+)\s [NC]
RewriteRule ^ https://newdomain.com/page%1/? [NE,R=301,L]
##Rules to redirect https://newdomain.com/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\s [NC]
RewriteRule ^ https://newdomain.com [NE,R=301,L]
I'm running into an issue where by my subdomains are incorrectly getting www appended to them via a htaccess rewrite rule...
My folder structure is as follows:
/public_html/index.html (A maintenance page just in case)
/public_html/.htaccess
/public_html/websitename
/public_html/subdomain
/public_html/testsite
/public_html/clone
My /public_html/ that's located in my .htaccess looks as follows
# Force HTTPS & WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} websitename.com$ [NC]
RewriteRule ^(.*)$ /websitename/$1 [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} subdomain.website.com$ [NC]
RewriteRule ^(.*)$ /subdomain/$1 [L]
</IfModule>
Which works perfectly for websitename.com, which forces the URL to be rewrote to https://www.websitename.com
However it makes my subdomains get incorrectly rewrote to https://www.subdomain.websitename.com when it should be https://subdomain.websitename.com
I don't want to have to put the forcing of HTTPS & WWW in the individual website folders... rather, I'm looking for a solution to make subdomain exempt from the rewrite rule.
I tried adding the following condition but it didn't help:
RewriteCond %{HTTP_HOST} subdomain.websitename.com$ [NC]
Anyone have any idea what I can do to get around this issue?
Remove:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
And add:
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
For multiple domains you can use:
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteRule ^ https://www.%1.%2%{REQUEST_URI} [R=301,L]
I needed to redirect my od domain to a new one. All paths are same on both domains except the front page, which needed to be redirected from www.mydomain.com to www.mydomain2.com/newpath. I googled and came up with this code which works. My question is if it is valid and if all pageranks will be transfered without problems. Thank you
RewriteEngine on
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.domain1.com/folder/ [L,R=301]
RewriteRule ^(.*)$ http://www.domain1.com/$1 [L,R=301]
Your code should work but it can be fine tuned a bit. Please consider this refactored code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
Rewriterule ^$ http://www.domain1.com/folder/ [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ http://www.domain1.com%{REQUEST_URI} [L,R=301]
I want the user to be redirected whenever he reaches my subdomain
Here is what is inside my htaccess:
RewriteEngine On
RewriteRule ^http://smale.deals.com/(.*) http://traual.deals.com/$1 [R=301,L]
RewriteRule ^http://deals.com/smale/(.*) http://deals.com/traual/$1 [R=301,L]
But no redirect happens. why?
I also have got this in my root htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (Android|iPhone|iPod|Blackberry) [NC]
RewriteCond %{REQUEST_URI} !^/mobile [NC]
RewriteRule ^(.*)/?$ mobile/$1 [L]
You cannot include the protocol and domain in RewriteRule. Those need to be accounted for in RewriteCond:
RewriteEngine On
# Rewrite requests to smale.deals.com to traual.deals.com
RewriteCond %{HTTP_HOST} ^smale\.deals\.com$ [NC]
RewriteRule (.*) http://traual.deals.com/$1 [R=301,L]
# For deals.com...
RewriteCond %{HTTP_HOST} ^deals\.com$ [NC]
# Rewrite requests to smale/ to deals.com/traual/
RewriteRule ^smale/(.*) http://deals.com/traual/$1 [R=301,L]
Suppose all the requests on A.com and B.com end up on the same server, and I want to control the request using htaccess.
The default www content root is /public_html/, but I want A.com requests to be forwarded to /public_html/A/ and B.com requests should be forwarded to /public_html/B/
I came up with this solution:
#/public_html/.htaccess
# A.com
RewriteCond %{HTTP_HOST} ^A.com$
RewriteCond %{REQUEST_URI} !^/A/
RewriteRule (.*) A/$1 [L]
# B.com
RewriteCond %{HTTP_HOST} ^B.com$
RewriteCond %{REQUEST_URI} !^/B/
RewriteRule (.*) B/$1 [L]
and I am having two problems with it:
A.com/A/index.php and A.com/index.php are the same thing, which is not cool! I'd rather the user be 301-ly redirected to the latter whenever he uses the former.
A.com/etc redirects to A.com/A/etc/
Overall, I don't want my visitors to see the /A/ (or /B/) in the URL anyway. Any htaccess solution is welcome.
You can use this code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# for external redirection of A.com/A/foo to A.com/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/*(A|B)(?:/(.*)|)\s [NC]
RewriteRule ^ http://%1.com/%2 [R=301,L,NC]
# for internal redirection of A.com/foo to A.com/A/foo
RewriteCond %{HTTP_HOST} ^(A|B)\.com$ [NC]
RewriteRule (?!^(A|B)(?:/(.*)|)$)^.*$ %1%{REQUEST_URI} [L]
Add this to your .htaccess in the same order:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^(A|B)(?:/(.*))?$ http://%{HTTP_HOST}/$2 [NC,L,R=301]
#/public_html/.htaccess
# A.com or B.com
RewriteCond %{HTTP_HOST} ^(A|B).com$ [NC]
RewriteRule (.*) %1/$1 [L]
If you do not want A to be redirected to A when accessed like this A.com/B/. or If you do not want B to be redirected to B when accessed like this B.com/A/. You can throw a 404 error.
RewriteCond %{HTTP_HOST} ^A.com$ [NC]
RewriteCond %{REQUEST_URI} ^/B(?:/(.*))? [NC]
RewriteRule ^ - [L,R=404]
RewriteCond %{HTTP_HOST} ^B.com$ [NC]
RewriteCond %{REQUEST_URI} ^/A(?:/(.*))? [NC]
RewriteRule ^ - [L,R=404]
Add the above lines right afterRewriteRule ^ - [L] before RewriteRule ^(A|B)(?:/(.*....