htaccess rewrite condtion only for specific domain not sub folder - apache

My current code is this
Redirect 301 /index.html http://example.com/
Redirect 301 /about.html http://example.com/
Redirect 301 /contact.html http://example.com/contact.php
However this is affecting another domain that points to a sub folder of this site.
is there a way to say something like if(DOMAIN != 'example2.com'){...}
i also use other RewriteRule functions like this
RewriteRule ^events/?$ events_page.php [L]
and also this to remove www
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Can't think of a particularly elegant way, but the following should do the job:
RewriteCond %{HTTP_HOST} !^example2\.com
RewriteRule /(index|about)\.hmtl http://example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} !^example2\.com
RewriteRule /contact\.hmtl http://example.com/contact.php [R=301,L]

Related

how to 301 redirect from root domain to a page with htaccess

i am trying to auto 301 redirect from domain.com to domain.com/v2
ive tried this code
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^$ /v2/ [NC,L]
also tried
Redirect 301 / /v2/
i get the error 'site has tried to redirect you too many times'
is there any way to do this?
thanks for any help
Untested but this should redirect both domain.com and www.domain.com to the v2 subfolder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ v2 [L]
You need to replace example.com with your actual domain because of posting restrictions.
I suppose you have an endless redirect because you are not matching the end of the domain string with $. Redirection should only take place at the root domain.
you can use a write rule with ^$ representing the root of the site and rewrite it to the selected folder
RedirectMatch ^/$ /v2/
it redirects the root and only the root URL.
or also you can use
RewriteEngine On
RewriteRule ^$ /v2[L]
If you want external redirection(301) you can use R flag
RewriteRule ^$ /v2[L,R=301]
Another way to use it with RewriteEngine
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ v2 [L]
Original Answers
I hope I've helped

301 redirects dynamic URL's htacces

I'm having trouble figuring 301 redirecting this.
Old site has a structure like this for subcategories:
www.domain.com/?someid=1&pageload=serieslist&someparam=1
What I would like to do is redirect some of the subcategories to new categories 1:1, and then the rest to new domain root.
Will it work doing it like this, first listing all 1:1 redirects, and then a "catch all" for the rest?
RewriteEngine On
RewriteBase /
Redirect 301 /?someid=1&pageload=serieslist&someparam=1 http://www.newdomain.com/category1/
Redirect 301 /?someid=2&pageload=serieslist&someparam=1 http://www.newdomain.com/category2/
RewriteCond %{QUERY_STRING} ^pageload=serieslist$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.dk/ [R=301,L]
You cannot use Redirect directive to match query string. Use it like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} someid=1&pageload=serieslist&someparam=1 [NC]
RewriteRule ^$ http://www.newdomain.com/category1/? [L,R=302]
RewriteCond %{QUERY_STRING} someid=2&pageload=serieslist&someparam=1 [NC]
RewriteRule ^$ http://www.newdomain.com/category2/? [L,R=302]
RewriteCond %{QUERY_STRING} (^|&)pageload=serieslist(&|$) [NC]
RewriteRule ^ http://www.newdomain.dk/? [R=301,L]

Redirect subdomain to main domain in Joomla

I need to redirect all subdomains of my website to the main domain starting with www:
I have tried to add this code to my .htaccess:
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
but I have a redirect loop error.
What do you suggest?
Thanks
Your rule will indeed cause redirect loop. Have it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
If you want demo.domain.com to be redirected to www.domain.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301,NE]
I'm using this code in my .htaccess file at the root directory of the domain or subdomain
Redirect all requests from subdomain:
Redirect / http://www.example.com/
Redirect all requests from folder sub on the domain to the subdomain:
Redirect /sub http://sub.example.com/

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]

Apache redirect subdomain to folder, keep parameters

I have this code in .htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
# If empty subdomain, replace with "www"
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]
# If subdomain isn't empty and not "www", redirect to "folder"
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301]
#PAGES REDIRECTION
RewriteRule ^(.*)/register/ /index.php?sub=$1&page=register
RewriteRule ^(.*)/register /index.php?sub=$1&page=register
RewriteRule ^(.*)/lostpass/ /index.php?sub=$1&page=lostpass
RewriteRule ^(.*)/lostpass /index.php?sub=$1&page=lostpass
...
(a rule for wildcard subdmains is already in place and working)
If I browse to http://test.example.com it redirects correctly to http://www.example.com/test but when I try to browse to http://test.example.com/register, it actually redirect to http://www.example.com/test/index.php?sub=http://www.example.com/test&page=register which should redirect to http://www.example.com/test/register
What am I doing wrong here? Thanks in advance!
Try adding the L flag to your second redirect rule, similar to how have it in the first.
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301,L]
It looks like your rewritten URI is passing through to the next rule.
Also, I don't think your first two RewriteCond are in the correct spot.