mod_rewrite: url rewriting plus subdomain (wildcard) rewrite at the same time - .htaccess

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.

Related

RewriteRule rule in .htaccess for matching keyword

I need to make a 301 redirect from
https://www.example.net/sub1/specific_keyword/page1/page2
https://www.example.net/sub2/sub3/specific_keyword/page3/page4
https://www.example.net/specific_keyword/page5/page6
to
https://www.example.net/sub1/
https://www.example.net/sub2/sub3/
https://www.example.net/
I tried this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.net$ [NC]
RewriteRule ^(.*)\/specific_keyword$ "https\:\/\/www\.example\.net\/$1" [R=301,L]
But no luck.
With your RewriteRule attempt, you demanded that the requested URL
ends with /folder-to-remove, via the $ at the end, so that won’t match (Source: #comment120998408_68464303)
With that fixed, place following rules at top of your htaccess Rules file. Make sure to clear your browser cache before testing your URLs or use a redirect checker online.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.net$ [NC]
RewriteRule ^([^/]*)/([^/]*)/.*$ $1/$2? [R=301,NE,L]
OR only match the specific keyword
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)specific_keyword/ https://www.example.com/$1 [R=301,L,NE]

htaccess mutliple rewrite rules for multiple domains

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?

rewriting subdomains with the full url

i would like to redirect 3 different urls, all in one htaccess file. But at the moment it isn't working and only the first rule is always triggered.
I would like to redirect the following:
service.abc.info to www.abc.de
service.abc.info/feedback/abc/ to feedback.abc.info/abc/
service.abc.info/feedback/def/ to feedback.abc.info/def/
For 2. and 3. I also need to redirect the complete Request URI.
Here is my .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^service\.abc\.info$ [NC]
RewriteRule .* http://www.abc.de [R=301,L]
RewriteCond %{HTTP_HOST} ^service\.abc\.info/feedback/abc/$ [NC]
RewriteRule .* http://feedback.abc.info/abc/{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^service\.abc\.info/feedback/def/$ [NC]
RewriteRule .* http://feedback.abc.info/def/{REQUEST_URI} [R=301,L]
The %{HTTP_HOST} variable holds the contents of the Host: request header, which just contains a hostname. No URI paths. You'll need to match the paths in the pattern of the rewrite rule, additionally, since the first rule is a superset of the other two, you need to move that to the end.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^service\.abc\.info$ [NC]
RewriteRule ^/?feedback/abc/(.*)$ http://feedback.abc.info/abc/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^service\.abc\.info$ [NC]
RewriteRule ^/?feedback/def/(.*)$ http://feedback.abc.info/def/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^service\.abc\.info$ [NC]
RewriteRule ^(.*)$ http://www.abc.de/$1 [R=301,L]

Redirect all non www to www except one subdomain with htaccess

I'd like to redirect all URLs not containing www to www
I used this:
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*) http://www.example.com$1 [QSA,L,R=301]
Now I need one subdomain not to be redirect like this, or I get an error.
So I'd like to redirect any URL that does not begin with sub or with www to www.example.com
How can I do this ? thanks
You need a second RewriteCond. You can apply as many as you like to a RewriteRule.
Assuming anything that is not sub.mydomain.com needs to be www.mydomain.com, here is your code:
RewriteCond %{HTTP_HOST} !^sub.mydomain.com$
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*) http://www.mydomain.com/$1 [QSA,L,R=301]
But you can simplify this further using the pipe (|) character in Regex:
RewriteCond %{HTTP_HOST} !^(sub|www).mydomain.com$
RewriteRule ^(.*) http://www.mydomain.com/$1 [QSA,L,R=301]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This works for all domains, excluding any subdomains. have fun.
I tried the answer by Scott S but it didn't work, so I modified to use the one I was using for general 301:
RewriteCond %{HTTP_HOST} ^domain.com
RewriteCond %{HTTP_HOST} !^subdomain.domain.com
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
And it works like a charm

.htaccess Wildcard Subdomains

I'm trying to implement a solution using .htaccess and wildcard subdomains so that
http://subdomain.example.com is mapped to http://example.com/index.php/accounts/subdomain/. My rules look something like:
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*/) /index.php [PT,L]
Which works, but disregards everything else. When I try appending anything to the rule e.g:
RewriteRule ^(.*/) /index.php/hello [PT,L]
I get a 500 internal server error. How do I get this working?
You probably need to exclude the index.php from your rule:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]
This is an adaptation of the code I use to redirect subdomains on my own site. I make no claims to it being best practice but it works;
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.example$ [NC]
RewriteRule ^.*$ http://www.example.com/index.php/accounts/%1/ [R=301,L]
Try changing your RewriteRule to
RewriteRule ^/(.*)$ /index.php/accounts/%1/$1 [PT]
That will rewrite the URL to one that includes the subdomain and the original request URI.
EDIT: maybe it needs to be
RewriteRule ^(.*)$ /index.php/accounts/%1/$1 [PT]
as mentioned in the comments.

Resources