I have multiple urls and want to redirect old urls to their new ones.
The code below does work, so for example htaccess is catching www.domain-a.com/index.php?id=41 and redirects it with a 301 status code to the same domain with the path www.domain-a.com/our-conditions:
#301 redirect for domain A on a special id.
RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$
RewriteCond %{REQUEST_URI} ^\/index\.php$
RewriteCond %{QUERY_STRING} ^id=41(.*)$
RewriteRule .* https://%{HTTP_HOST}/our-conditions? [R=301,L]
#410 on for domain A on a special id.
RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$
RewriteCond %{REQUEST_URI} ^\/index\.php$
RewriteCond %{QUERY_STRING} ^id=52(.*)$
RewriteRule .* - [R=410,L]
#301 redirect for domain B on a special id.
RewriteCond %{HTTP_HOST} ^www\.domain-b\.com$
RewriteCond %{REQUEST_URI} ^\/index\.php$
RewriteCond %{QUERY_STRING} ^id=221(.*)$
RewriteRule .* https://%{HTTP_HOST}/press? [R=301,L]
As you can see above that's a lot of code for just a few rules.
Now I thought that can be done better and I tried something different.
My idea was to have multiple RewriteRules for one domain. Usually that works just fine like this example here:
RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$
RewriteRule ^press/material/(/?)$ https://%{HTTP_HOST}/press [R=301,L]
RewriteRule ^about/(/?)$ https://www.domain-b.de/impress [R=301,L]
Now i came up with this tryout:
#Multiple rules for domain A on a special id.
RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$
RewriteCond %{REQUEST_URI} ^\/index\.php$
RewriteRule ^index.php?id=41(/?)$ https://%{HTTP_HOST}/our-conditions? [R=301,L]
RewriteRule ^index.php?id=52(/?)$ - [R=410,L]
#Multiple rules for domain B on a special id.
RewriteCond %{HTTP_HOST} ^www\.domain-b\.com$
RewriteCond %{REQUEST_URI} ^\/index\.php$
RewriteRule ^index.php?id=221(/?)$ https://%{HTTP_HOST}/press? [R=301,L]
The problem here is that my conditions do not apply. For example index.php?id=41 is just going trough htaccess and my application does say 404 (not found).
Can you help me out here to get my approach working?
Related
How to redirect with domain
site.com/image/some_symbols
to subdomain
some_symbols.site.com/
I've tried so
RewriteCond %{HTTP_HOST} ^(www\.|)site\.com$ [NC]
RewriteCond %{REQUEST_URI} ^image/(+*)$
RewriteRule ^(.*)$ http://%1.%{HTTP_HOST}/$ [R=301,L]
The %{REQUEST_URI} starts from the / at the beginning; which is why your pattern was not successful.
Better yet, do not rely on another RewriteCond, keeping it simply as:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?site\.com$
RewriteRule ^image/(.+)$ http://$1.%{HTTP_HOST}/ [R=301,L]
I have two domain names for the same website, and need to do a correct rewrite, so that whenever someone accesses the first domain name and all subdirectories, with, or without www. they get redirected to the second domain and subdirectories without www.
I managed to set the redirect for the domain name without subdirectories, but for whatever reason, subdirectories are not getting rewritten.
So when I go to domainnamenumberone.com, or www.domainnamenumberone.com, i get redirected to domaintwo.com – however, when I go to domainnamenumberone.com/wordpress/path or www.domainnamenumberone.com/wordpress/path I remain there, and nothing gets rewritten.
Here's what I placed in .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
Would be grateful for your help!
You need to place this rule as very first rule in DocumentRoot/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainnumberone\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(\S*)\s [NC]
RewriteRule ^ http://domaintwo.com/%1 [L,R=302,NE]
Then add this line in each child .htaccess like wordpress/ or wordpress/path/ (wherever .htaccess already exists) below RewriteEngine On line
RewriteOptions Inherit
You can use that:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
But it seems to me that it should also work with yours.....
try a different browser (cache problem)
i have an htaccess for creating virtual subdomains which working fine
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt|JPG)$
RewriteRule ^(.*)$ virtialsubdomain.php?subdomain=%1 [L]
With the above i can see for example virtual1.domain.com, virtual2.domain.com etc
I want to extend the htaccess so i can pass 2 parameters for paging and sorting
Example
virtual1.domain.com/p/3 show page 3
virtual1.domain.com/sort/id,asc show results sort by id asc
virtual1.domain.com/sort/id,asc/p/3 the combination of 2 above
For other pages in the www.domain.com i have rules that achieve that
RewriteRule category/([^.]+)/sort/([^.]+)/p/([0-9]+)?$ /file.php?id=$1&sort=$2&p=$3 [L,NC,QSA]
RewriteRule category/([^.]+)/sort/([^.]+)?$ /file.php?id=$1&sort=$2 [L,NC,QSA]
RewriteRule category/([^.]+)/p/([0-9]+)?$ /file.php?id=$1&p=$2 [L,NC,QSA]
But i really dont know how to write the rules when comes the virtual subdomains
Any help appreciated
I always separate things out first. Make all request to subdomains map to the subdomains folder like so:
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^subdomains/
RewriteRule ^(.*)$ /subdomains/$1 [L]
Then in /subdomains/ you create a separate htaccess to do the paging and sorting rules.
RewriteBase /subdomains
RewriteRule ^p/([0-9]+)$ index.php?page=$1 [L,QSA]
RewriteRule ^sort/([^/]+)(/p/([0-9]+))?$ index.php?sort=$1&page=$3 [L,QSA]
I recently got an alternate domain (parked) and would like it to behave like this:
on a request for "alt-domain.com/" (root): serve "main-domain.com/fakeroot" without a redirect (200)
on a request for "alt-domain.com/anyotherpage": serve "main-domain.com/anyotherpage" with a redirect (301)
I tried this, but it doesn't work:
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.)$ /fakeroot.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
Each rule works on its own, but when both are present the request for root also get a redirect. I tried inverting the order, tried skipping [S=1], tried [NS], but no luck. That's when I realized I'm not an htaccess expert, nor a regex expert.
Any help would be much appreciated.
D.
The problem is that the L flag probably doesn't work like you expect, and when mod_rewrite re-examines your rules, RewriteCond %{REQUEST_URI} !^/$ matches because the %{REQUEST_URI} is updated to /fakeroot.php after the initial rewrite.
There are a few different ways to fix this, but I believe this should work well enough, and it doesn't involve changing much:
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.)$ /fakeroot.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/(fakeroot\.php)?$
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
I have two rules in my .htaccess file for url rewriting:
for subdomain rewriting: xxx.domain.com is internally redirected to file.php?item=xxx
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]
RewriteRule ^$ /file.php?item=%2 [QSA,nc]
ordinary rewriting:
RewriteRule ^([A-Za-z0-9_)(:!-',]+)/?$ file.php?item=$1 [L]
What i need to achieve is writing a third rule that will combine these two rules without being in conflict with them. Namely, below (or above) this lines i need to have something like that:
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com/([A-Za-z0-9_)(:!-',]+)$ [NC]
RewriteRule ^$ /anotherfile.php?item1=%2&item2=$1 [QSA,nc]
so that http://xxx.domain.com/yyy will be redirected to anotherfile.php?item1=xxx&item2=yyy
any ideas that will work, or what is the correct way of it?
Try this rule:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com$ [NC]
RewriteRule ^([A-Za-z0-9_)(:!-',]+)/?$ /file.php?item1=%2&item2=$1 [QSA]
It uses conditions of the first rule and URL path pattern of the second.