3

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.:

  1. public.example.com => www.example.com // OK
  2. public.example.com/any-existing-file.pdf => stays at public.example.com/any-existing-file.pdf // OK
  3. public.example.com/any-not-existing-file.pdf => www.example.com // OK
  4. public.example.com/anything-except-certain-url-below => www.example.com // OK
  5. public.example.com/certain-url => need to stay at public.example.com/certain-url, but it is redirected at www.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]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
sylar32
  • 197
  • 3
  • 16
  • You must have some other rules in play here, or you are seeing a _cached_ redirect from an earlier (erroneous) rule. 301s are cached persistently by the browser (and possibly intermediary caches). The rules you've posted would only redirect the root of the `public` subdirectory to www. Nothing more. These rules do not redirect `public.example.com/anything` to `www`. Are you behind a front-end proxy that serves your static resources? That could explain why `public.example.com/any-existing-file.pdf` is not redirected. – MrWhite Jan 06 '23 at 20:39
  • There should not be any other rules . This is full content of my .htaccess. Before every use I clear the cache, so it should not be by it either. About the last question, unfortunatelly I do not know. It is common webhosting and I do not have any special rights. – sylar32 Jan 07 '23 at 13:20
  • @MrWhite so please could you give me rules by you which should do what I need according examples in my first post? I am going to replace the current ones by them and let's see if it will work. – sylar32 Jan 07 '23 at 13:25
  • To clarify, you want to redirect to the www homepage only, the original URL-path is removed? – MrWhite Jan 07 '23 at 19:15
  • @MrWhite yes, I do not need the original url-path after redirection. – sylar32 Jan 07 '23 at 23:57

1 Answers1

2
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.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • 1
    Thank you for you respond. Redirecting PDF files work just fine. But the `certain-url` is still redirected. I think the problem will be in the very last group of rules I've posted in my first post. When I comment them, the `certain-url` page will stay in browser, but I've got server error 404 (there is no rule to transform seo-url to ugly-one). When I uncomment them back, the `certain-url` page is redirected to `https://www.example.com/?page=certain-url` . I clear the cache everytime before I test new change in htaccess. – sylar32 Jan 12 '23 at 14:19
  • 1
    @sylar32 Ah yes, sorry overlooked that... the rewritten URL is being redirected during the second pass by the rewrite engine (since this is different to `/certain-url`). You need to add an additional _condition_ to the redirect. I've updated my answer. – MrWhite Jan 12 '23 at 16:14