Stopping URL rewrite if request is from specific subdomain - .htaccess

Is there a way to stop .htaccess from rewriting a URL if it is from a specific domain?
For example:
example.com/test usually rewrites to example.com/pages/test.php
beta.example.com/test will NOT rewrite. So goes to beta.example.com/test (same)
Basically, what is the rule that looks for a subdomain and stops all rewrites after it?

Add this to the top of your htaccess file:
RewriteCond %{HTTP_HOST} ^beta\.example\.com$ [NC]
RewriteRule ^ - [L]
This will make it so if the request is for beta.example.com it passes the URI through the entire rewrite engine without messing with it.

Related

How to use .htaccess to redirect while maintaining the original URL

I am trying to use the .htacces file to redirect any files or subfolders in a derectory to one file, while still maintaining the original URL input.
So if a user goes to:
https://example.com/folder1/folder2/folder3/
or
https://example.com/folder1/folder2/file.php
it would redirect them back to:
https://example.com/folder1/index.php
but the original URL input would not change.
You can use RewriteRule . In htaccess in the document root add the following rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder1/index\.php$ [NC]
RewriteRule ^folder1/.+$ /folder1/index.php [L]
This will redirect /folder1/foo/bar to /folder1/index.php without changing the url in browser.
The RewriteCond above makes sure you don't rewrite /folder1/index.php to itself (/folder1/index.php) otherwise the rule can cause an infinite loop error.
You can just make :
RedirectMatch 301 ^https://example.com/folder1/ https://example.com/folder1/index.php
This allows you to redirect from the first url in the pattern to the
second one

How can I redirect match full subdomain urls, but make the root subdomain go elsewhere using htaccess

I have a bunch of old urls from a subdomain I need redirected to another subdomain and retain the url pattern. The issue I'm struggling with is getting just the root subdomain to redirect to another location.
For context, here is an example of the old url structure:
https://oldsub.maindomain.com/2015/07/30/url-title/
Which I need redirected to:
https://blog.maindomain.com/blog/2015/07/30/url-title/
Notice it needs to be directed to a new blog subdomain and there is a blog slug added after the main domain.
If anyone visits just the root of the old subdomain (https://oldsub.maindomain.com/) I need that redirected to a URL structure on just the main domain (https://maindomain.com/specific-url-title/)
I have my .htaccess within the root of the subdomain directory.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsub\. [NC]
RewriteCond %{HTTP_HOST} ^(?:oldsub\.)?(.+)$ [NC]
RewriteRule ^ https://blog.%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{HTTP_HOST} ^oldsub\.maindomain\.com$ [NC]
RewriteRule ^ https://maindomain.com/specific-url-title/ [L,NE,R=301]
In the .htaccess of the root domain I have:
RewriteCond %{HTTP_HOST}
RedirectMatch 301 ^/blog/(.*)$ https://blog.maindomain.com/blog/$1
So the old blog posts redirect properly to the blog.maindomain.com/blog/rest-of-url but the last two lines aren't working as the old subdomain request simply goes to blog.maindomain.com/blog as well.
The file structure is:
/public_html (maindomain.com)
.htaccess
/oldsub.maindomain.com (oldsub.maindomain.com)
.htaccess
blog.maindomain.com lives on another server (on hubspot platform)
Your current redirects are not correct, so these need to be fixed. (Although you should have seen a redirect of some kind.)
RewriteCond %{HTTP_HOST} ^oldsub\. [NC]
RewriteCond %{HTTP_HOST} ^(?:oldsub\.)?(.+)$ [NC]
RewriteRule ^ https://blog.%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{HTTP_HOST} ^oldsub\.gppcpa\.com$ [NC]
RewriteRule ^ https://maindomain.com/specific-url-title/ [L,NE,R=301]
The problems with these directives:
The first rule does not redirect to the /blog subdirectory.
These rules conflict. The first rule will also redirect the document root of the subdomain. Consequently, the second rule will never be processed.
Since the subdomain in question is completely outside of your maindomain, in terms of where these hostnames point to on your filesystem, then there is no need to check the hostname in these directives since only the oldsub subdomain can access this area.
Try the following instead:
RewriteRule . https://blog.maindomain.com/blog%{REQUEST_URI} [R=302,L]
RewriteRule ^ https://maindomain.com/specific-url-title/ [R=302,L]
The single dot in the first RewriteRule pattern matches "something" (not "nothing"). When the URL-path is empty (ie. the document root) this falls through to the second directive.
Make sure you clear your browser cache before testing.
Test with 302 (temporary) redirects and only change to 301 (permanent) when you are sure it's working OK. This is to avoid any caching issues, since 301s are cached persistently by the browser (including those made in error).
RewriteCond %{HTTP_HOST}
RedirectMatch 301 ^/blog/(.*)$ https://blog.maindomain.com/blog/$1
From your maindomain .htaccess file... That RewriteCond directive is erroneous here and should be removed.
To avoid potential conflicts with mod_rewrite you should change the mod_alias RedirectMatch directive to mod_rewrite. For example:
RewriteEngine On
RewriteRule ^blog/(.*) https://blog.maindomain.com/blog/$1 [R=302,L]
Different Apache modules (mod_rewrite, mod_alias, etc) run at different times throughout the request and run independently of each other. mod_rewrite usually runs first (regardless of the apparent order of the directives in your config file), but this also means that mod_alias (Redirect, RedirectMatch) will always run, even though an earlier mod_rewrite directive (RewriteRule) might have rewritten the URL and stopped execution.

Rewrite and Redirect with htaccess

A client has asked us to replicate all the content from an old domain (bcsbd.com) to their main domain (ywcacam.org), and also create redirects so the old URLs are still functional. Unfortunately, the URLs aren't exact matches, e.g., [olddomain]/about has become [newdomain]/about_soo_bahk_do. There are less than 10 specific URLs to handle, which we initially did successfully using Redirect statements in the old domain's htaccess file:
# redirect specific pages to page on new domain
Redirect /about http://www.ywcacam.org/about_soo_bahk_do
We also need a catch-all, so that any other requests go to a specific URL on the new domain, e.g., www.bcsbd.com/somefile becomes www.ywcacam.org/soo_bahk_do. We handled this using Rewrite statements:
# catch-all for any requests not specified above
RewriteCond %{HTTP_HOST} ^(bcsbd.com|www.bcsbd.com) [NC]
RewriteRule ^(.*)$ http://www.ywcacam.org/soo_bahk_do [L]
Quick research showed the Rewrite directives (using mod_rewrite) would always be processed before the Redirect directives (using mod_alias). So we replaced the Redirects with Rewrites:
Options +FollowSymlinks
RewriteEngine On
RewriteRule /about http://www.ywcacam.org/about_soo_bahk_do [L]
RewriteRule /programs http://www.ywcacam.org/programs_soo_bahk_do [L]
...
# catch-all for any requests not specified above
RewriteCond %{HTTP_HOST} ^(bcsbd.com|www.bcsbd.com) [NC]
RewriteRule ^(.*)$ http://www.ywcacam.org/soo_bahk_do [L]
The problem is that just the catch-all is working - the new Rewrite rules are being ignored. What are we doing wrong in those statements?
Thanks in advance for the help!

htaccess redirects from URLs on one domain to the root of another domain

Currently what is happening is people are accessing old URLs from google like icpaweb.com/site/pages/about-us/ and being sent to their corresponding urls on icpaweb.org : icpaweb.org/site/pages/about-us.
What I want is to send people from: icpaweb.com/site/pages/about-us to icpaweb.org/ without any of the succeeding url segments.
How do I do this?
If you have to use an .htaccess file, you can use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} icpaweb.com$ [NC]
RewriteRule .* http://icpaweb.org/ [R=301,L]
That will 301 redirect all requests for icpaweb.com to the index root of icpaweb.org. If you don't want 301, it can just be R.
You'll need to replace or turn off whatever mechanism is doing your redirecting now, they may not be compatible.
Use an url rewrite rule.
2 steps:
Write a RewriteCond so that the following rewrite rule only apply for url with host being icpaweb.com like RewriteCond %{HTTP_HOST} icpaweb.com$ [NC] The [NC] is for case insensitive match
Write a rewrite rule that convert all input to what you want like RewriteRule ^.*$ http://icpaweb.org/ [L]The [L] is to stop the rewriting to this rule if rule executed.

HtAccess Rewrite Needed

My host will not allow me to change the default folder of my primary domain. I have managed to Rewrite http://www.mysite.com to the real folder
public_html/mysite.com/www/
with the following code:
RewriteEngine On
RewriteRule ^$ /mysite.com/www/ [R=301,L]
This does successfully load my domain from the subfolder, but the url becomes:
http://mysite.com/mysite.com/www/
How can I continue loading requests from http://mysite.com/index.html in the correct folder shown above, without showing it in the client-side url?
Try this one:
RewriteEngine On
RewriteRule ^mysite.com/www/(.*) - [L]
RewriteRule ^(.*)$ mysite.com/www/$1 [L]
UPD:
The line with the dash is required because after the redirect at line 3 Apache reads the .htaccess once again to process the redirected URL. The rule prevents infinite loop.
Try removing the R=301.

Resources