I have two subdomains private
, public
(but public
is only alias of private
) and one main domain www
. I need to redirect all URLs from public
subdomain to www
, except existing PDF files and one URL address. I have these rules which work fine, but I can not add the exception for the one certain URL.
e.g.:
public.example.com
=>www.example.com
// OKpublic.example.com/any-existing-file.pdf
=> stays atpublic.example.com/any-existing-file.pdf
// OKpublic.example.com/any-not-existing-file.pdf
=>www.example.com
// OKpublic.example.com/anything-except-certain-url-below
=>www.example.com
// OKpublic.example.com/certain-url
=> need to stay atpublic.example.com/certain-url
, but it is redirected atwww.example.com
// KO
I have these rules in my .htaccess
file.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^public\.example\.com$
RewriteRule ^$ http://www.example.com/ [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^private\.example\.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !\.[[:alnum:]] $
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ %{REQUEST_URI}/ [R=301,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(. )/?$ ?page=$1 [QSA,L]
CodePudding user response:
RewriteCond %{HTTP_HOST} ^public\.example\.com$ RewriteRule ^$ http://www.example.com/ [R=301,L]
This is the relevant rule in question. However, as written (and as mentioned in my earlier comment), this only redirects requests for the document root (as denoted by the ^$
regex). ie. This rule only redirects http(s)://public.example.com/
to http://www.example.com/
(why http
?) and nothing more.
The only way you can be seeing a redirect for scenarios #3, #4 and #5 is if you have (or had) a different rule that did this and you are perhaps now seeing a cached redirect. 301 (permanent) redirects are cached persistently by the browser.
However, you are also routing requests for all non-existent files through your front-controller (last rule), except you have neglected to include the actual file that handles the request (I assume index.php
since you have tagged the question php
), so you are reliant on the DirectoryIndex
being set correctly. Consequently, your front-controller might also be initiating these redirects (in PHP)?
To perform the necessary redirects in .htaccess
you would need to change the above rule to something like the following instead, splitting it into two rules:
# Skip the next rule if requesting a PDF file that exists
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.pdf$ - [S=1]
# Redirect public to www except "/certain-url"
RewriteCond %{HTTP_HOST} ^public\.example\.com [NC]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule !^certain-url$ http://www.example.com/ [R=301,L]
The 2nd condition that checks against the REDIRECT_STATUS
environment variable ensures that we only check direct requests from the client and not rewritten requests by the front-controller pattern (last rule). The issue you were facing (stated in comments) is that the rewritten URL was being redirected (since this is not /certain-url
).
Note that the URL-path matched by the RewriteRule
pattern does not start with a slash.
Test first with a 302 (temporary) redirect to avoid potential caching issues. And you will need to clear your browser (and any intermediary caches) before testing.
But, as stated, you likely still need to resolve where these redirects were coming from in the first place, since they are not from the rules as posted in the question.