Consider this URL:
www.sub1.sub2.sub3.subdomain.domain.com
The only thing that is a given is "domain.com". Now, I want to forward this UGLY url to: subdomain.domain.com. So somehow I have to get the subdomain.
I tried:
RewriteCond %{HTTP_HOST} ^(.+)/.([^.]+)/.domain.com$ [NC]
RewriteRule ^(.*)$ http://%2.domain.com/$1 [L,R=301]
Which won't work, as htaccess doesn't support look behind.
Any workaround for this problem?
You can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.+?\.([^.]+\.domain\.com)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
Related
I need a hand with creating a regex rewrite rule for my .htaccess.
Here's the problem. My previous URLs structure was:
http://example.com/whatever-the-url-is/?lang=en
now I turned it into
http://example.com/en/whatever-the-url-is/
I'd like to create an .htaccess URL that 301 redirects all the URLs from the previous structure to the new one.
Also the .htaccess is shared between different domains, and so there should be
RewriteCond %{http_host} !^example.com$ [NC] condition at the beginning...
Is it possible? Do you have any suggestions?
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(.+?)(?:&|$) [NC]
RewriteRule ^(.*)$ /%1/$1? [R=301,L]
You can add the RewriteRule after RewriteCond %{HTTP_HOST}:
RewriteCond %{REQUEST_URI} !^/(?:en|fr|sp)/ [NC]
Where you test if langcode is already in the url.
I want to be able to redirect url such as domain.com/cat-1/long-tail-kw.html to new domain and url such as differentdomain.com/goodname.php?id=long-tail-kw
Also, the cat-1 will change often, it could also be cat-271 or whatever number.
How can I do this in .htaccess file?
Also, the redirect must work with this code that sends the people who come from bing ONLY to the new url.
RewriteEngine On
RewriteCond %{HTTP_REFERER} (bing)
RewriteRule ^(.*)$ http://differentdomain.com/goodname.php?id=long-tail-kw
Thanks!
You can insert a new rule for this:
RewriteEngine On
RewriteCond %{HTTP_REFERER} (bing)
RewriteCond %{HTTP_HOST} ^(?:www\.)?sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://differentdomain.com/goodname.php?id=long-tail-kw [L,QSA,R=302]
RewriteCond %{HTTP_HOST} ^(?:www\.)?sub\.domain\.com$ [NC]
RewriteRule ^cat-\d+/(.+?)\.html$ http://differentdomain.com/specificname.php?id=$1 [L,QSA,R=302]
In the RewriteCond you can use the regular expression. So I think you can try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} domain.com
RewriteCond %{REQUEST_URI} ^cat-([0-9]+)long-tail-kw\.html$
RewriteRule ^(.*)$ http://differentdomain.com/goodname.php?id=long-tail-kw
My current rewrite rule:
RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?a=$1&v=$2&id=$3 [L]
The result above works great so I can format the URL like
domain.com/a/b/c
I would like to add in a domain switch as well so the results I want is
sub.domain.com/a/b/c when you access it using domain.com/a/b/c
Currently here is what I have tried
RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?a=$1&v=$2&id=$3 [L]
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule (.*)$ http://sub.domain.com/ [R=301,L]
But the result of this is
http://sub.domain.com/a=a&v=b&id=c
and needs to be
http://sub.domain.com/a/b/c
Thanks for the help!!
Reverse the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule (.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)?$ index.php?a=$1&v=$2&id=$3 [L,QSA]
Make sure to test this after clearing your browser cache.
I want to redirect couple of pages to https. As I've already use one condition to redirect all requests to http, for those coupoe of pages, it shows too many redirects.
Look at my code below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^login\.php$ https://www.example.com/login.php
This doesn't work. "Too many redirects".
Anyone can help?
The first rule makes no sense.
Shorten to this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^login\.php$ https://www.example.com/login.php [L,R]
If the idea of the first rule was to force www, add this:
# From http://stackoverflow.com/a/4958847/1078583
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I want to make mod rewrite as the following example :
from google.com.mydomain.com to mydomain.com/index.php?domain=google.com
I use like
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{QUERY_STRING} !domain=
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain.com$ [NC]
RewriteRule ^(.*)$ /index.php?domain=%1 [L,QSA]
but this redirect google.mydomain.com to mydomain.com/index.php?domain=google
I want rule to redirect google.com not google
thanks
It is due to the incorrect regex. Try this rule:
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{QUERY_STRING} !domain=
RewriteCond %{HTTP_HOST} ^(.+?)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ /index.php?domain=%1 [L,QSA]
In your rule you're using [^\.]+ which matched until a dot is found therefore it is matching google instead of google.com.