Rewriting a subdomain to root/subdomain - regex

I currently have the following location: localhost/dir1/ex.html
I'm trying to achieve a situation where dir1.localhost/ex.html maps to that first url. The following .htaccess rule works:
RewriteCond %{HTTP_HOST} =dir1.localhost
RewriteRule ^(.*)$ http://localhost/dir1/$1 [L]
However, this is not what I am looking for as it quite obviously redirects. I want to keep the url the same (dir1.localhost).
How would one go about doing this? Many thanks in advance.

If you want your subdomain to point to the folder internally ,Remove the hostname from your rule's destination :
RewriteCond %{HTTP_HOST} =dir1.localhost
RewriteCond %{REQUEST_URI} !^/dir1
RewriteRule ^(.*)$ /dir1/$1 [L]`

Related

Redirect all urls except one in .htaccess

I am trying to redirect all URLs on my site (let's call it www.site1.com) except one in my .htaccess file. I currently have the following in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/my/page$ [NC]
RewriteRule (.*) https://www.site2.com [R=302,L]
With the above, all requests to www.site1.com are redirected to www.site2.com, including the one that I do not want to redirect.
After some experimentation, I have found that the following works to redirect only a specific page:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/my/page$ [NC]
RewriteRule (.*) https://www.site2.com [R=302,L]
I'm not sure why the ! operator isn't working as I expect. Perhaps there is an error in my regex?
Some additional bit of information. This is a Drupal site running on a dedicated cPanel host. But I have been sure to put this new redirect rewrite rule before all the other Drupal-specific rewrite rules.
Any help would be greatly appreciated.
RewriteCond %{REQUEST_URI} !/(index.php) [NC]
RewriteRule ^(.*)$ index.php [NC,QSA,L,END]
You need to make sure you exclude the page you redirect to from you list of possible url that get redirected(to avoid the loop) and then you can add more before the RewriteRule.
RewriteCond %{REQUEST_URI} !/(index.php) [NC]
RewriteCond %{REQUEST_URI} !/(index_new.php) [NC]
RewriteRule ^(.*)$ index.php [NC,QSA,L,END]

RewriteCond when REQUEST_URI do not match htaccess apache2

I have a multilingual wordpress website and want to redirect website of given region to given language,
xyz.de --> xyz.de/de/
xyz.co.uk --> xyz.co.uk/en/
direct access to xyz.de/de and xyz.co.uk/en are working properly. So there is no problem on wordpress side.
Now, I am trying to change the htaccess file of xyz.de and xyz.co.uk so that they redirect the website.
Considering xyz.co.uk
I want to add a RewriteCond such that whenever there is no /en trailing after xyz.co.uk it will automatically add /en.
For example xyz.co.uk/<trailing address> results in xyz.co.uk/en/<trailing address>
So far I have the following code, which somehow doesn't seem to work,
RewriteCond %{REQUEST_URI} !^/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
The negation of /en is not working! I have also tried
RewriteCond %{REQUEST_URI} !/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
Could someone tell me where I am going wrong? seems like I have gone wrong in writing RegEx and suggest if there is better way to achieve the same, that does not affect the SEO across different domains.
Use THE_REQUEST variable instead of REQUEST_URI:
RewriteCond %{HTTP_HOST} \.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !/en/ [NC]
RewriteRule ^ /en%{REQUEST_URI} [L,R=302,NE]
Make sure to keep this rule as your very first rule in .htaccess.
Change it to R=301 once you've tested.

.htaccess redirect with parameters and multiple directory levels

I need to redirect from this URL:
www.devsite.com/level1/page1.html?brand=6
to:
www.productionsite.com/level1/page1.html?brand=6
I've come across various redirect w/ parameters answers here on stack, but none that specifically address how to rewrite part of .htaccess to redirect to a totally different domain. Help!
Just do a redirect from devsite to productionsite. Second line appends after the devsite domain to the new location.
RewriteCond %{HTTP_HOST} !^www.devsite.com$ [NC]
RewriteRule ^(.*)$ http://www.productionsite.com/$1 [L,R=301]
Add this to the htaccess file in the document root of your www.devsite.com domain. Add it above any other rules that may already be in that file.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?devsite\.com$ [NC]
RewriteRule ^level1/page1\.html$ http://www.productionsite.com/level1/page1.html [L,QSA]
RewriteCond %{QUERY_STRING} \?brand=6
RewriteCond %{HTTP_HOST} ^www\.devsite\.com$
RewriteCond %{SERVER_PORT}s ^(443(s)|\d+s)$
RewriteRule ^level1/page1\.html$ http%2://www.productionsite.com/$0 [R=301,QSA,L]

Add GET parameter in htaccess

I am looking for a way to add a rule to the htaccess that will automatically add a GET parameter to a request coming in on a subdomain (the reason why is a long story and seems irrelevant).
So sub.example.com should redirect to sub.example.com?parameter=test
The problem I'm having is the circular reference. There must be a way to avoid this?
You can try this code:
RewriteEngine On
RewriteCond %{QUERY_STRING} !(^|&)parameter=test(&|$) [NC]
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule ^ %{REQUEST_URI}?parameter=test [L,R=301,QSA]

Subdomain redirection with htaccess and a 500 Internal Server Error

My .htaccess file is, for some reason, causing 500 Internal Server Errors when I'm trying to redirect a subdomain like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com [NC]
RewriteRule ^(.*)$ folder/anotherfolder/$1 [L]
My current host doesn't redirect subdomains by themselves, so I have to do it this way. What I'm trying to achieve is that when a user enters http://subdomain.domain.com/, he really sees http://domain.com/folder/anotherfolder. I, however, don't want the domain itself on the address bar to change. The subdomain itself is, apparently, an alias of the main domain and thus points to the same folder as the main domain.
The above code also works when the regex result it used as a GET, as such:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com [NC]
RewriteRule ^(.*)$ folder/anotherfolder/index.php?p=$1 [L]
The value of the GET "p", however, is not e.g. "test.php" (if you tried to access subdomain.domain.com/test.php), but the whole path from the main domain (i.e. folder/anotherfolder/test.php).
Any ideas what I'm doing wrong?
Ah, seems to have been caused by an internal redirection loop. Fixed by doing this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.domain.com [NC]
RewriteRule ^folder/anotherfolder/ - [L]
RewriteRule (.*) folder/anotherfolder/$1 [L]
Funny how the answer usually reveals itself right after you've asked someone.
Change your code to this:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^p= [NC]
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule ^(.*)$ folder/anotherfolder/index.php?p=$1 [L]