Apache mod_rewrite - want a file system redirect not a 301 redirect - apache

I have example1.com on a shared web host running Apache. It has a directory example1.com/foo. I now want example2.com to serve the same content from example1.com/foo, except at the example2.com root without the intervening directory in the URL. Like example2.com/bar.html should serve the same content as example1.com/foo/bar.html .
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule ^(.*)$ foo/$1 [NC]
This simple rewrite rule takes any request intended for example2.com and inserts the foo/ to point to the content which is in that directory. Problem is this keeps doing an external 301 redirect. I don't want that, I want the browser to stay on example2.com without redirecting while Apache serves up the content from /foo in the filesystem.
Been over the Apache mod_rewrite docs several times, which say how to force a 301 redirect with the [R] flag but don't say how to force it NOT to happen. What am I missing here? It is behaving the same on both my Linux shared host and a local test with Apache on Windows.
Thanks!

I figured this out. The 301 was happening because I had the directory name wrong in the rule. So the result of the rule pointed to a path that didn't exist, which makes Apache try to fallback from the file system redirect to a 301 redirect.
Then I had to fix an infinite loop, since that above rule always adds "foo" to the URL even if it's already present so I'd get foo/foo/foo/foo/... . We need to add it only if it's not already there. Had to do it with this two-step rule, because you can't use wildcards in a capturing group of a negative rule. But this seems to work, adding "foo" when the host is example2.com and the URL does not already contain "foo".
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule !^foo - [C]
RewriteRule ^(.*)$ foo/$1 [NC,PT]

Related

Apache redirect for adding missing .html at the end

I want to redirect any request that doesn't have the last segment contain a dot to .html
examples:
/travels/
/travels.html
/travels/categories/new-zealand
/travels/categories/main.js
Line 2 and 4 should not be redirected while line 1 and 3 should be redirected to:
/travels.html
/travels/categories/new-zealand.html
I already have this regex that seems to work for capturing: ^(https?:\/\/.+(\..+)*\/)?[^\s.]+$
How do I exactly make this redirect happen? I have a vserver with an apache werbserver that is running and mod_rewrite is enabled. How exactly would the rewrite statement look and where do I put it? If I put it in a .htaccess file, where do I keep that? Inside the root of the page or anywhere?
You can not match against host and https header in the rule's pattern. You can only match against URL path in a RewriteRule . To check the host and https header you need to use RewriteConds
RewriteEngine on
# URL scheme is HTTPS
RewriteCond %{HTTPS} on
# Host is www.example.com or example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
#URL path is /travels/ then redirect the request to /travels.html
RewriteRule ^/?travels/$ /travels.html [R,L]
This will redirect https://example.com/travels/
to https://example.com/travels.html . This changes the URL in your browser from old to the new one, if you want the redirection to happen internally then just remove the R flag.

How to add a rewrite rule for domain and it's subdomains in Plesk 11.0.9?

I need to redirect a request file from all possible subdomains to one file which is located in top level domain:
^.example.com/robots.txt >> http://example.com/robots.txt
The robots.txt may or may not exist in the subdomain httpdocs, this rule must be exucuted first (we want to block all crawlers in advance).
The folder structure provided by Plesk 11.0.9 is:
example.com/
...
httpdocs/
subdomains/
sub1/
httpdocs/
example.com is owned by root.
Where can I add an apache rewrite rule that would be trigger for all subdomains?
Edit: I tested with ping doesntexist.example.com and the request does get directed to example.com which means teoretically there should be a point in processing to execute the rewrite logic.
Try placing this rule in /example.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.(example\.com)$ [NC]
RewriteRule ^robots\.txt$ http://%1/$0 [L,R=302,NC]

redirect additional subdomain in htaccess

I have a problem with an redirect.
I have this code in my htaccess file.
RewriteCond %{HTTP_HOST} ^frutasadomicilio.cl
RewriteRule ^ http://www.frutasadomicilio.cl%{REQUEST_URI} [L,R=301]
Redirect 301 /index.html http://www.frutasadomicilio.cl/venta-de-verduras
Redirect 301 http://frutasadomicilio.cl http://www.frutasadomicilio.cl/venta-de-verduras
This code is working fine. This code redirects all requests without www to www.
But my problem is when I try to access resources.frutasadomicilio.cl that is a subdomain, I am redirected to www.frutasadomicilio.cl/venta-de-verduras.
I assume your subdomain points to a subdirectory off the document root of the main domain (a common setup)? eg. resources. -> /resources/...?
The problem in this case is that the .htaccess file from the parent directory on the filesystem is inherited. So, a request for resources.example.com will result in a redirect (Redirect 301 /index.html ...).
Also, you should not mix mod_alias redirects (ie. Redirect) with mod_rewrite redirects (ie. RewriteRule). If you are using mod_rewrite then only use mod_rewrite.
Redirect 301 http://frutasadomicilio.cl http://www.frutasadomicilio.cl/venta-de-verduras
This line actually does nothing and should be removed. Redirect takes a root-relative path (starting with a slash) as the source path, not an absolute path, so this will never match.
The last two Redirects can be rewritten as the following (using mod_rewrite) to avoid your unwanted redirection:
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(index\.html)?$ /venta-de-verduras [R=301,L]
Now, the redirection only occurs when the host starts www. (so excludes resources.).
Alternatively, you can omit the above RewriteCond directive and include an .htaccess file in the /resources subdirectory with the following directive.
RewriteEngine On
mod_rewrite directives are not inherited by default, so by simply enabling the rewrite engine in the subdirectory prevents the mod_rewrite directives in the parent .htaccess from being executed (note that other modules in the parent .htaccess file are still processed). This might be preferable if you have a lot of redirects in the parent .htaccess file that need to be excluded.

htaccess redirect domain and mask url

I want to redirect a domain name : http://www.newdomain.com to http://sub.maindomain.com
I have edited .htaccess as:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !sub.maindomain.com$ [NC]
RewriteRule ^(.*)$ http://sub.maindomain.com/$1 [L,R=301]
It redirects but the http://sub.maindomain.com visible in the address bar . Help! How to do the masking so that http://www.newdomain.com would be visible . Thanks
And also I want create a subsite http://sub.newdomain.com . How to do it with .htaccess .
If www.newdomain.com and sub.maindomain.com are on different hosts, the only way to get the content on sub.maindomain.com to be served while the browser's URL address bar says www.newdomain.com is to fetch the content from sub.maindomain.com on behalf of the browser. Since as far as the browser is concerned, it's looking at content on newdomain.
You can do this either by setting up a proxy using apache or maybe a script. Using apache, you'd need access to your vhost config for the www.newdomain.com domain. You can set it up like:
ProxyPassMatch ^(.*)$ http://sub.maindomain.com$1
Take a look at the mod_proxy docs for more info on the flags you can pass this directive, to tweak timeouts and retries.
If you want to setup a php script to proxy, you'll need to route all requests to this php script, so in your htaccess file in your www.newdomain.com document root, add (assuming this php script is called proxy.php):
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/proxy.php
RewriteRule ^(.*)$ /proxy.php?url=$1 [L,QSA]
Then in proxy.php, look for the url query string, load that page, and output it to the browser.
Though all of this is much easier if you can just point both domains to the same host.

Apache rewrite from root folder, force https

I would like to redirect traffic from mysite.com to mysite.com/folder, but also enforce SSL on all pages.
Currently we have this in httpd.conf we found from a sample somewhere, but I don't think that it is working correctly all of the time.
RewriteEngine on
RewriteRule ^/$ https://www.mysite.com/folder/ [R]
The ^/$ only matches if the request is for exactly /, i.e. the root of the site. Anything else is not matched by it. Try .* instead.
Note: if this rewrite rule is active for both the HTTP and the HTTPS version, that'll send you into an infinite redirect loop. In that case, you may need some kind of RewriteCond test, too.