redirect based on directory in path via .htaccess - apache

Via .htaccess, how do you redirect this
http://www.somedomain.com/de/foo
to this:
http://www.de-domain.com/foo
The redirection should depend on the second parameter, in the example above "de".

Depending on the server configuration you should be able to do this on the .htaccess for somedomain.com. If this works appropriately you might try [R=301,L] to make the redirection permanent:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?somedomain.com$
RewriteRule ^([a-z]{2})/(.*) http://www.$1-domain.com/$2 [R,L]

Related

Apache mod_rewrite from a folder to a different domain doesn't work with trailing slash

I'm trying to rewrite URLs from old.domain.tld/project to domain.tld/subfolder/project using .htaccess in the project directory on the old server. I think I need to check for the host name so the rule only applies to the old server, even if the .htaccess file is also put on the new server.
I've tried the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteRule ^(.*)$ https://domain.tld/subfolder/project/$1 [R=301,L]
This works fine for URLs like https://old.domain.tld/project/index.php?p=q (redirects to https://domain.tld/subfolder/project/index.php?p=q) but not for the URL without the trailing slash—https://old.domain.tld/project ends up being redirected to https://domain.tld. Very odd! How do I make this work for both types of URL?
I tried a rather different method to make this a generic redirect, that could be included even if the folder name wasn't project, but this has the same problem:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule . https://domain.tld/subfolder/%1 [R=301,L]
In a server-wide configuration, a slash is appended to all requests for index files, e.g. old.domain.tld/project is redirected to old.domain.tld/project/. Could this be causing a problem?

apache redirect to remove a subfolder from the url

I have issues redirecting a specific url within the same domain
Example, I want to redirect
http://example.tld/folder/*
to
http://example.tld/*
where * is a wildcard
Basically you will need this type of code in your .htaccess file:
RewriteEngine On
RewriteRule ^/folder/ http://domain.tld/? [R=301,L]
This should redirect your current url http://domain.tld/folder to http:/domain.tld/
Or:
RewriteEngine On
RewriteRule ^folder/(.*)$ /$1 [R=301,NC,L]
Please try and see which one works best for you

Disguising URL by rewriting htaccess

I have a site built for a client located on my server (ie http://www.myserver.com/clientsite). Can I disguise the URL by modifying the .htaccess file on my client's host (http://www.clientsite.com) to read as his domain, but display my content and keep the subdirectories in tact?
So: http://www.myserver.com/clientsite would read http://www.clientsite.com
and http://www.myserver.com/clientsite/about would read http://www.clientsite.com/about
I tried the following, but it was directing me to a 404 error.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^clientsite.com
RewriteRule ^(.*) http://www.myserver.com/clientsite/$1 [P]
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?clientsite.com
RewriteRule ^(.*) http://www.myserver.com/clientsite/$1 [P]
Since you mentioned that your client's site is http://www.clientsite.com and Rewrite condition was applied only to clientsite.com , I changed it to support both with and without www.
If it doesn't work, put your rewrite log and I'll check that.

apache .htaccess file https:// redirect ---- rewrite it for subdirectory?

here is my code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
This will successfully rewrite everything to https (it will "force" https), if I put it in my home directory's .htaccess file. However, what about if I only want my subdirectory of /support to force https? How to rewrite the above code?
The intent is with regards to forcing https in WHMCS
Thanks in advance!
If you want to redirect only a subfolder and not a subdomain, that is even easier. To redirect the subfolder /support/ you would use the following code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^/support/(.*)$ https://%{HTTP_HOST}/support/$1 [QSA]
I added a QSA flag, which will force any query string that was entered to be appended to the redirect URL. If you don't use query strings in this section of the site, you can remove that flag. Also, if the string as shown doesn't work, you can try removing the leading slash from the RewriteRule and retrying it.
Add the following rule before the %{HTTPS} line:
RewriteCond %{HTTP_HOST} ^support.domain.com$
This will only allow the rule to execute if the visitor is coming from the subdomain. Please note, these rules will likely need to be put in the .htaccess file that is the root for the subdomain, not the main site root folder.

301 redirect .htaccess

Im trying to request the following entire site 301 redirect:
word.something.blah.domain.com --> http://www.word.com
I don't know how to write the 301 redirect rule.
Can someone help out?
I will assume you are using the same directory to serve files on both domains. In which case, a Redirect clause won't work (infinite redirect loop).
With mod_rewrite, you can check the value of the current HTTP_HOST and take a decision based on that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.something\.blah\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%1.com/$1 [R=301,NE,L]
put this into root directory of the subdomain:
Redirect permanent / http://www.word.com
If you are keeping everything else the same - that is, the file names - but simply changing the domain, this code is all you need to put on the OLD DOMAIN htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.newdomain\.co.uk
RewriteRule (.*) http://www.newdomain.co.uk/$1 [R=301,L]