htaccess redirect domain and mask url - apache

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.

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.

redirect exact url to another url

There are a bazillion examples online of doing redirects via apache's htaccess, but I can't find any example of redirecting a full URL match to another full URL.
For instance, I have an existing website at
https://example.com
How do I redirect some specific URLs for that domain to a different one:
https://example.com/login --> https://my.example.com/login
https://example.com/register --> https://my.example.com/register
For every other every other path on example.com I need to remain untouched so my site still works fine (i.e. /blog shouldn't redirect somewhere else).
You can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(login|register)/?$ http://my.example.com/$1 [L,R]
This will redirect example.com/login or example.com/register to http://my.example.com/
You can alternatively accomplish this using the RedirectMatch directive (if the newurl is on a diffrent webserver) :
RedirectMatch ^/(login|register)/?$ http://my.example.com/$1

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

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]

apache configure site maintainance page

I am trying to configure in apache httpd.conf for the below scenario. Can anyone please advise how to do it.
When the apache instance is up and some one tries to hit below url
http://mydomain.com/xxx
It should display content exists in sitemaintenance.html without changing url context path.
Assume sitemaintenance.html resides in /usr/local/apache/my-apache-instance/htdocs/sitemaintenance.html
Here xxx is a subdomain which is hosted separately from main domain (mydomain.com).
-KS
Try placing this into httpd.conf or .htaccess for your sub-domain:
RewriteEngine on
RewriteCond /usr/local/apache/my-apache-instance/htdocs/sitemaintenance.html -f
RewriteRule ^.*$ /sitemaintenance.html [PT,L]
The condition in RewriteCond enables rewrite only if that file exists, so you can dynamically add or delete it, without re-starting Apache or changing .htaccess.

Redirecting PDF links from another domain using htaccess

We have two domains, let's call them first.com and second.com
We have a directory in second.com called reports, where all our PDFs are located, but we would like to these same PDFs accessible from first.com as well.
Can we redirect let's say first.com/reports/84839049.pdf to second.com/reports/84839049.pdf using htaccess?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain1.com/$1 [R=301, L]
Yes.
redirect /requested/url http://second.com/result/url
http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirect
You may want to consider using mod_rewrite though, unless you asked for an .htaccess configuration specifically because you have no access to the server configuration and mod_rewrite is disabled or not loaded.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://webdesign.about.com/od/mod_rewrite/qt/site_redirects.htm
You'll need some grasp of regex for mod_rewrite, but it can make configuration of the redirects a lot faster than having to add a redirect for every file on your site(s).