mod_rewrite apache FQDN to Hostname - apache

I'm trying to find a mod_rewrite code for forcing users that go to FQDN to be re-written to the hostname without the domain and don't want them to be stuck in a loop. I can only find re-write examples for the other way round. Anyone have a suggestion on how I can do this?
So example if I was to go to http://appname.example.com/test.php
The rewritten URL should be http://appname/test.php
Any suggestions would be great.

To "redirect" http://<appname>.example.com/<url-path> to http://<appname>/<url-path> (where <appname> and <url-path> are entirely variable and <appname> is also a resolvable hostname on the local network) then you would do something like the following using mod_rewrite at the top of the root .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)([a-z-]+)\.example\.com [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]
This excludes the www subdomain using a negative lookahead.
%1 in the substitution string is a backreference to the <appname> as captured from the requested hostname.
%{REQUEST_URI} contains the full root-relative URL-path (starting with a slash).
This also handles FQDN (that end in a dot).

Related

htaccess Rewrite rule from "example.com/de/foo" to "example.de/foo"

RewriteRules can be such a pain for me, I cannot get this one to work.
I have to redirect urls like example.com/de/anypath to example.de/anypath.
[anypath] can be really any path, as I have to get it work for
example.com/de/articles/programming/hello-world (would be redirected to example.de/articles/programming/hello-world)
as well as for example.com/de/events/pic-nic (would be redirected to example.de/events/pic-nic).
This is what I wrote so far :
RewriteRule "^/de/(.*)$" "http://example.de/$1" [R=301,NC,L]
I also tried with RewriteCond with no more luck
RewriteCond %{HTTP_HOST} http://example.com/de
I am working with xampp, but tested on my web server with same
result.
I know this .htaccess file is working (get error if I enter a
typo)
I got some result when testing with something like :
RewriteRule "^(.*)$" "https://google.com" [R=301,NC,L]
Any help would be appreciated !
RewriteRule "^/de/(.*)$" "http://example.de/$1" [R=301,NC,L]
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash. ie. It should be "^de/(.*)$", not "^/de/(.*)$".
You don't need the double quotes and the NC flag is probably redundant, unless you also need to match dE, Ed or DE.
For example (near the top of the root .htaccess file):
RewriteRule ^de/(.*) http://example.de/$1 [R=301,L]
(HTTP, not HTTPS?!)
The trailing $ on the RewriteRule pattern was also redundant.
Test first with 302 (temp) redirect to avoid potential caching issues.
RewriteCond %{HTTP_HOST} http://example.com/de
The Host HTTP request header (ie. the value of the HTTP_HOST server variable) contains the hostname only. eg. example.com only in your example.
Any server variable that is prefixed with HTTP_ refers to the HTTP request header of the same name.
I got some result when testing with something like :
RewriteRule "^(.*)$" "https://google.com" [R=301,NC,L]
Careful with testing 301s since they are cached persistently by the browser. You will need to clear your browser cache before testing!
I added two conditions (the first is to apply according to local or live site, the second to leave the node paths unchanged) :
RewriteCond %{HTTP_HOST} local.example.com
RewriteCond %{REQUEST_URI} !^.*/de/node/.*$
And it is working as expected. Thanks again.

Redirect >> domain.com/test to another url (domain.com/test3 using .htaccess (wp)

I am trying to redirect one complete URL
But this is not working ...
RewriteCond %{HTTP_HOST} ^https://www.moccioso.com/d.php?
id=ultimateselection.zip$
RewriteRule ^$ http://moccioso.com/sd_product/ultimateselection-zip
[L,R=301]
(I'm assuming you've not wrapped the code onto multiple lines in your actual code?)
The HTTP_HOST variable contains the Host HTTP request header. ie. the hostname specified on the request eg. www.example.com only - there is no scheme, URL-path or query string here. So, this RewriteCond directive will never match.
But you are also trying to match an empty URL-path with the RewriteRule pattern ^$ - so again, this will never match. In your example the URL-path is /d.php. The RewriteRule pattern matches against the URL-path only.
You are also redirecting to http (not https) in this directive? Your example uses https. You are also removing the www subdomain, but again, your example includes it?
If you are redirecting from/to the same hostname and only have one domain then you don't need to explicitly include the domain name (ie. the requested hostname) in the rule.
To match against the query string you need to match against the QUERY_STRING server variable.
Try the following instead, near the top of your .htaccess file.
RewriteCond %{QUERY_STRING} ^id=ultimateselection\.zip$
RewriteRule ^d\.php$ /sd_product/ultimateselection-zip [R=301,L]
This will need to go near the top of your .htaccess file. Test first with a 302 (temporary) redirect to avoid potential caching issues.
Clear your browser cache before testing.

Apache redirect dynamically one domain to another

We are currently migrating from one domain to another, I need to write a rewrite rule that will redirect all websites from the old domain to the new one.
I can match if the browser try to access the old domain :
RewriteCond "%{HTTP_HOST}" "^(.*).old.com$" [NC]
Now I need the rewrite rule that can extract the host before ".old.com" to create the new URL with "xx.new.com". It seems to me that a RewriteRule only contains the path of the page being accessed and not the full URL that would contain the host part.
I would like to do something like this :
RewriteRule "^https:\/\/(.*)\.old\.com(.*)$" "https://$1.new.com/$2" [R=301,L]
How can this be achieved?
Any help would be greatly appreciated.
Your condition is good but your rewrite rule is wrong: the regex pattern will be matched against the part of the URL after the hostname and port, and before the query string, so there is no hostname there.
Still, your substitution can back-reference to the pattern matched in RewriteCond:
RewriteCond "%{HTTP_HOST}" "^(.*).old.com$" [NC]
RewriteRule ^ "https://%1.new.com%{REQUEST_URI}" [R=301,L]
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

how do I rewrite an url but include some of the same url content

I have incoming requests going to video.mydomain.com/myvideo.avi that need to be re-written to www.mydomain.com/myvideo.avi. This is in apache 2 on RHEL 6.
Thanks in advance.
Use mod_rewrite. If it's not already then you may have to install it in Apache. Add something like this to your VirtualHost conf file:
RewriteEngine On
RewriteCond %{HTTP_HOST} video\.([^\.]+\.[^\.]+)$
RewriteRule (.*) https://www.%1%{REQUEST_URI}
The regular expression in the RewriteCond line will match a pattern like video.mydomain.com but the parentheses around the [^.]+.[^.]+ part define it to be a capture group. The value of that is then found in %1 (since it's the first capture group you defined) and you can put that into the new URL in the RewriteRule part with the %1. Note that the example above will work with any domain name that starts with video.

Replace parts of the URL with mod_rewrite

I need a mod_rewrite rule to redirect url depending on the hostname they are comming from.
The situation:
We have multiple domains pointing to a same webspace and we need to restrict what the specific host can see/download.
domainname.com/images/logo.jpg and /www.domainname.com/images/logo.jpg should transform into domainname.com/domainname_com/images/logo.jpg
So basically I need a rule/function that replaces the dots in the %{HTTP_HOST} with _ and removes/replaces the www subdomain.
Is there any way to do this with mod_rewrite?
Try these rules:
RewriteCond %{ENV:DOMAIN_DIR} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^images/.+ - [E=DOMAIN_DIR:%2]
RewriteCond %{ENV:DOMAIN_DIR} ^([^.]*)\.(.+)
RewriteRule ^images/.+ - [E=DOMAIN_DIR:%1_%2,N]
RewriteCond %{ENV:DOMAIN_DIR} ^[^.]+$
RewriteRule ^images/.+ %{ENV:DOMAIN_DIR}/$0 [L]
The first rule will take the host and store it without www. in the environment variable DOMAIN_DIR. The second rule will replace one dot at a time; the N flag allows to restart the rewriting process without incrementing the internal recursion counter. Finally, the third rule will rewrite the request to the corresponding directory.