.htaccess rewrite/redirect directory to subdomain - .htaccess

In my website's root .htaccess file, I have the code shown here to redirect to the www version, as well as hide the index.php page if in the directory root:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]
RewriteRule ^index\.php$ http://www.example.com/ [R=301,NC,L]
Within this site, I have an API located at http://www.example.com/app/API/
I would like to do two things:
Perform all API calls via the subdomain http://api.example.com/ (but really process them at the original URL, http://www.example.com/app/API/)
Redirect all traffic from http://www.example.com/app/API/ to http://api.example.com/, keeping all query parameters in tact.
What I'm asking seems fairly simple, but for whatever reason I can't figure it out (even after extensive searching). I can set up the subdomain through my hosting provider, but I'd rather use .htaccess for further customization.

Try:
RewriteCond %{HTTP_HOST} !^api\. [NC]
RewriteRule ^app/API/(.*)$ http://api.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^api\.example\.com$ [NC]
RewriteRule ^(.*)$ /app/API/$1 [L]

Related

Using .htaccess to redirect to https, subfolder and prevent naked domain URLs?

I'm having a heck of a time constructing an .htaccess file to do everything that I need. I have a domain that is only hosting a forum in a /forums subdirectory. I have no landing page, nor do I intend to.
So I want to redirect the root domain to the /forums subfolder, force https and THEN PREVENT naked domains from being allowed.
I've got it all working except users can still manually access/choose a naked domain variant. Some users have been bookmarking the naked domain variant and I want to force them, and all users, back into the www. prefix.
The code below takes http:www.example.com or http:example.com and forces it to https://www.example.com/forums/. The problem is users are still able to manually access https://example.com/forums.
I've tried a bunch of different things to force the forums in the /forums subdirectory to always use a www. prefix, but nothing has worked thus far:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/forums [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/www\.example\.com\/" [R=301,L]
With your shown samples/attempts could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine on
##Handle non https all urls here.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
##Handle non www urls here.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
##Handle no uri link with no www page.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/?$ http://www.example.com/forums [R=301,L]
##Handle no uri link with www page.
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^/?$ https://www.example.com/ [R=301,L]

Mod rewrite redirect one file to a file on another domain

I have been at this for a number of hours and cannot get this to work. I have other redirects in this .htaccess file that do work.
I need to redirect as follows.
mydomain.com/dir/subdir/myfile.php to myotherdomain.com/dir/my_index.php
Note that mydomain.com and myotherdomain.com both point to the same root directory.
Here's my code.
RewriteCond %{HTTP_HOST} ^(www.)?postle.com$
RewriteCond %{REQUEST_URI} !^/pi_www/hardface/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /pi_www/hardface/$1
RewriteCond %{HTTP_HOST} ^(www.)?hardfacetechnologies.com$
RewriteRule ^(/)?$ postle_hft/index_hft.php [L]
I have tired all the permutation of this I can think of and nothing works. I really would appreciate some help.
You can use this rule in /dir/subdir/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^myfile\.php$ http://myotherdomain.com/dir/my_index.php [R=302,NC,L]
If you want to avoid external redirect then:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^myfile\.php$ /dir/my_index.php [NC,L]
should also work as both domains point to same site root.
In the .htaccess file in your document root, try:
RewriteCond %{HTTP_HOST} =mydomain.com
RewriteRule ^dir/subdir/myfile\.php$ http://myotherdomain.com/dir/my_index.php [R=302,L]
The order of directives is important. This should come after your canonical www redirect, but before any internal rewrites, and before any redirects that might conflict.
This also assumes that your canonical URL is the bare domain (ie. not the www subdomain) - like with the example in your question. To match the bare domain or the www subdomain (and make it case-insensitive) then change the RewriteCond directive to:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com$ [NC]
Change the temporary (302) redirect to permanent (301) when you are sure it's working OK. (301 redirects are cached by the browser, so makes testing troublesome.)

Using ISAPI-Rewrite htaccess to 301 redirect multiple urls for multiple hostnames?

I have a single site within IIS, which runs on 4 different hostnames. The CMS handles that, and displays the correct site based on the incoming hostname.
I need to use ISAPI rewrite to handle all the old urls and 301 redirect them to the new equivalants, this is how I currently deal with redirects in my ISAPI-rewrite .htaccess file
RewriteEngine on
RewriteRule ^post/my-old-page-one$ /my-newer-page-one [R=301]
RewriteRule ^post/my-old-page-two$ /my-newer-page-two [R=301]
My issue is, I need it to check the domain too for the incoming url. As the sites have the same old urls, which now need to go to a different page.
I was hoping I could so this.
RewriteEngine on
RewriteRule ^http://www.siteone.com/post/my-old-page-one$ http://www.siteone.com/my-newer-page-one [R=301]
RewriteRule ^http://www.siteone.com/post/my-old-page-two$ http://www.siteone.com/my-newer-page-two [R=301]
RewriteRule ^http://www.sitetwo.com/post/my-old-page-one$ http://www.sitetwo.com/my-newer-page-one [R=301]
RewriteRule ^http://www.sitetwo.com/post/my-old-page-two$ http://www.sitetwo.com/my-newer-page-two [R=301]
But it doesn't work. Any advice greatly appreciated.
The check of the hostname is performed in a separate condition, like this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.siteone\.com$ [NC]
RewriteRule ^post/my-old-page-one$ http://www.siteone.com/my-newer-page-one [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.siteone\.com$ [NC]
RewriteRule ^post/my-old-page-two$ http://www.siteone.com/my-newer-page-two [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.sitetwo\.com$ [NC]
RewriteRule ^post/my-old-page-one$ http://www.sitetwo.com/my-newer-page-one [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.sitetwo\.com$ [NC]
RewriteRule ^post/my-old-page-two$ http://www.sitetwo.com/my-newer-page-two [R=301,NC,L]

.htaccess mod_rewrite redirection not working

I am using the following condition/rule on a .htaccess file located at the root of my domain. The purpose is to redirect all non-www requests to their www. version:
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
It seems to be working fine.
Inside my /blog/ subdirectory I have another .htaccess that I use to redirect fancy URLs to real ones:
RewriteCond %{THE_REQUEST} !.*?url=.*
RewriteRule (.*) index.php?url=$1 [QSA]
This also seems to be working fine. However, all non-www requests inside the /blog/ subdirectory are not being redirected to their www. version.
For example, if I type domain.com on the browser I correctly get redirected to www.domain.com. But if I type domain.com/blog/ or domain.com/blog/test-page/ I won't get redirected to the www. version.
Probably the .htaccess inside /blog/ is conflicting with the one at root level, but I don't know how or how to fix it. Any clues?
Update: I solved the problem by putting all the rules on the root .htaccess file. I had to tweak the fancy URL rules slightly to only catch the /blog/... requests. Here's the final .htaccess file in case it might help you:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^apprush\.com$ [NC]
RewriteRule ^(.*)$ http://www.apprush.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/blog
RewriteCond %{REQUEST_URI} !.*?url=.*
RewriteRule blog/(.*) blog/index.php?url=$1 [QSA,L]
Are you able to put all of the rewrite rules into the root level .htaccess? It not only gets around the problem you're having but is a little neater because you know exactly where all of your rules are located.
If you are, these rules will do what you need:
RewriteCond ${HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond ${THE_REQUEST} !/blog/.*?url=.*
RewriteRule (.*) index.php?url=$1
This is because you have dollar sign $ at the end of domain.com. Also you need to use REQUEST_URI instead of HTTP_HOST. Remove it:
RewriteCond %{REQUEST_URI} ^domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
The dollar sign signifies the end of the string, thus making the rewrite to work for direct requests to domain.com. The update script solves the problem.

HTACCESS redirection — old domain to new domain, *except* home page?

I'm using the following to redirect traffic from an old domain to a new one:
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
It works great, but I need to actually redirect traffic going directly to the old site's home page to a different location. How do I add this exception?
Try using the following:
## Redirect for home page requests
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /oldhomepage.html [R=301,L]
## Redirect for all other requests
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/oldhomepage.html
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
Depending on where you are sending the requests for the homepage, this may need to change some, but essentially, you need to check the content of %{REQUEST_URI} for each RewriteRule.
Hope this helps...

Resources