HTTPS redirect, exclude script parameters from URL

HTTPS redirect, exclude script parameters from URL - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the HTTPS redirect, exclude script parameters from URL error on your web browser. Problem :


My goals:



1. http://example.com/page/index.php -> https://example.com/page/
2. http://example.com/page/ -> https://example.com/page/
3. http://www.example.com/page/ -> https://example.com/page/
4. Any nonexistent file or directory requests -> index.php?&q=$1 for further processing.


The following rules work fine, but only after a redirect in the browser URL instead of a clean URL. For example, if I request:



https://example.com/page/


I see



https://example.com/index.php?&q=page/


How do I hide index.php?&q=page and show https://example.com/page/ in the URL instead? Before I added HTTPS redirect it worked fine for HTTP. I tried 2 solutions with the same result.



RewriteEngine on
RewriteBase /


Solution 1:



#1 solution:
RewriteCond %THE_REQUEST ^[A-Z]3,9 /index.php HTTP/ [NC]
RewriteRule ^index.php$ https://%HTTP_HOST/ [L,NE,R=301]

RewriteCond %SERVER_PORT 80 [OR]
RewriteCond %HTTP_HOST ^www. [NC]
RewriteCond %HTTP_HOST ^(?:www.)?(.+)$ [NC]
RewriteRule (.*) https://%1%REQUEST_URI [L,NE,R=301]

RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME !-f
RewriteRule (.*) index.php?&q=$1 [QSA]


Solution 2:



#2 solution:
RewriteCond %THE_REQUEST ^[A-Z]3,9 /index.php HTTP/ [NC]
RewriteRule ^index.php$ https://%HTTP_HOST/ [L,NE,R=301]

RewriteCond %HTTP_HOST ^www.(.*)$ [NC]
RewriteRule (.*) https://%1%REQUEST_URI [L,NE,R=301]

RewriteCond %SERVER_PORT 80
RewriteRule (.*) https://%HTTP_HOST%REQUEST_URI [L,NE,R=301]

RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME !-f
RewriteRule (.*) index.php?&q=$1 [QSA]

Solution :


How do I hide index.php?&q=page and show https://example.com/page/ in the URL instead? Before I added HTTPS redirect it worked fine for HTTP.




The code you posted should not result in such a "redirect". And neither should adding the "HTTPS redirect" have affected this.



Unless... you initially added the "HTTPS redirect" at the end of your .htaccess file, after the internal rewrite to index.php?&q=$1. In which case, you would have expected a request for either http://example.com/page/ or https://www.example.com/page/ or http://www.example.com/page/ to have resulted in such an external redirect (as opposed to an internal rewrite) to https://example.com/index.php?&q=page/. HOWEVER, you have stated that requesting https://example.com/page/ (already HTTPS and non-www) results in such a redirect - that would not happen under this scenario.



It's still possible that an earlier (erroneous) redirect has been cached (301's are persistently cached by the browser) and this is what you are seeing here. However, you've stated (in comments) that the (browser? and all intermediary?) cache(s) has been cleared. So, that just leaves "something else" (ie. something else external to the directives you've posted that sets a 3xx HTTP status)?






Additional notes, not directly related to your main problem:





  1. http://example.com/page/index.php -> https://example.com/page/



    RewriteCond %THE_REQUEST ^[A-Z]3,9 /index.php HTTP/ [NC]
    RewriteRule ^index.php$ https://%HTTP_HOST/ [L,NE,R=301]




Condition #1 is not satisfied by any of the directives you've posted. The directives you've posted above would only redirect a request for /index.php in the document root, not /page/index.php as you've stated. To redirect /page/index.php, you would naturally need to include the /page path segment when matching the requested URL (and pass this through to the substitution). For example:



RewriteCond %THE_REQUEST ^[A-Z]3,9 /page/index.php HTTP/ [NC]
RewriteRule ^(page/)index.php$ https://%HTTP_HOST/$1 [L,NE,R=301]


However, I suspect an "any directory" solution would be required. In which case you could do something like the following:



RewriteCond %THE_REQUEST /index.php HTTP/ [NC]
RewriteRule (.*)index.php$ https://%HTTP_HOST/$1 [L,NE,R=301]






RewriteRule (.*) index.php?&q=$1 [QSA]



You should probably include the L (last) flag on this directive, in case you add any more directives later in the file. (This would have prevented an erroneous redirect if you had added the HTTPS redirect at the end by mistake.)



And, unless you have a specific requirement, you should be rewriting to index.php?q=$1 (no & after the ?). For example:



RewriteRule (.*) index.php?q=$1 [QSA,L]

We hope that this article has helped you resolve the redirects, https, mod-rewrite error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

Redirected urls show in serp, with the original permalink but with the title and meta of the target page

How can I redirect everything but the index as 410?

How do I redirect traffic only if being accessed from a specific port?