RewriteCond if url is not "cdn.domain.com/images/" - .htaccess

we have created a CDN subdomain to host images, using this URL:
https://cdn.example.com/images/
What we want is to redirect, if someone goes to any other path of this subdomain. For example:
https://cdn.example.com/
https://cdn.example.com/blabla/
Redirect to other domain, just not redirect if the folder is images.

create htaccess file in your document root, and deny direct access to all files
Order deny,allow
Deny from all
and create htaccess file in images:
Allow from all

Try the following using mod_rewrite, near the top of your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^cdn.example.com [NC]
RewriteRule !^images/ https://example.com%{REQUEST_URI} [R,L]
If you access cdn.example.com/<something> and the URL-path does not start /images/ then redirect to https://example.com/<something> (the main domain).
Change R to R=301 (after you've made sure it is working OK) if you want a permanent redirect.
UPDATE: To exclude additional folders (eg. images2 and images3) then you can change the RewriteRule directive to:
RewriteRule !^(images|images2|images3)/ https://example.com%{REQUEST_URI} [R,L]
If the folders are literally called images, images2 and images3 then this could be simplified to match (or rather not match) any URL of the form /imagesN - where N is an optional digit:
RewriteRule !^images\d?/ https://example.com%{REQUEST_URI} [R,L]

Related

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.

Redirect specific URL to another URL on another subdomain

I have 5 URLs on a subdomain website http://subdomain.example.com, and I want them to redirect to 5 other URLs on my main website, https://www.example.com/.
Important: URLs do NOT have the same structure!
Example:
http://subdomain.example.com/url1 should redirect to https://www.example.com/ipsum
http://subdomain.example.com/url2 should redirect to https://www.example.com/lorem
etc.
How can I handle that?
UPDATE:
There is a play folder (name of the subdomain) which contains the subdomain website files and a htdocs folder which contains the www website files.
Here is the .htaccess file in my play folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Since the subdomain has it's own .htaccess file, you don't need to specify the hostname as part of the redirect. And since you already have mod_rewrite directives in the subdomain's .htaccess file, you should also use mod_rewrite for these redirects (to avoid conflicts). Otherwise, you'll need to specify these redirects one-by-one.
Try the following at the top of your subdomain's /play/.htaccess file. Note that this needs to go before the existing directives in the file.
# Specific redirects
RewriteRule ^url1$ https://www.example.com/ipsum [R=302,L]
RewriteRule ^url2$ https://www.example.com/lorem [R=302,L]
The above would match a request for http://subdomain.example.com/url1 and redirect accordingly, etc.
Note that the RewriteRule pattern (regular expression) does not start with a slash when used in a per-directory (.htaccess) context.
Note that these are 302 (temporary) redirects. Change them to 301 (permanent) - if that is the intention - only once you have confirmed they are working OK (to avoid caching issues).
Try this...
Redirect 301 http://subdomain.example.com/url1 https://www.example.com/ipsum
Redirect 301 http://subdomain.example.com/url2 https://www.example.com/lorem

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 for root pages / but not sub-directories or sub-domains

trying to rewrite URL strings only for pages that reside in the root, removing the page extension for cosmetic reaons. For example:
www.site.com/page.html ==> www.site.com/page
www.site.com/about.html ==> www.site.com/about
Using this code currently:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+\.com) [NC]
RewriteRule (.*) http://www.%1/$1 [R=301,L]
BUT, I don't want the rule to modify sub-directiories or sub-domains. For example I also have:
clientA.site.com (which is mapped to www.site.com/clientA), which I need to not be remain unchanged. Right now it is sending that page (client.site.com/index.html) to a page not found.
Your rule doesn't affect subdomains. There must be other rules causing the 404 not found response.
If you want to restrict requests to the root pages, you can specify that with an appropriate pattern by excluding slashes in the request URL path
# exclude directories
RewriteCond %{REQUEST_FILENAME} !-d
# allow top level request URLs only
RewriteRule ^[^/]*$ http://www.%1/$0 [R,L]
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.

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