Mobile app version of vmapp.org
Login or Join
Vandalay111

: Using htaccess to internally and externally redirect a page at the same time I've been trying to figure this out for two hours, and I give up. I want to externally redirect about.php to about/,

@Vandalay111

Posted in: #FileExtension #Htaccess #Redirects #TrailingSlash

I've been trying to figure this out for two hours, and I give up. I want to externally redirect about.php to about/, and also have about/ internally redirect to about.php

RewriteRule ^about/$ /test/about.php [L]
RewriteRule ^about.php$ /test/about/ [R,L]


Both of these rules work fine by themselves, but when they're combined they cause an endless loop (since each rule undoes the other one).

How can I combine them without causing an endless loop?

EDIT: In the "related" tab it shows a question that's exactly the same as this that I just couldn't seem to find myself through Google, and the solution works perfectly:

Using a .htaccess to RewriteRule and Redirect 301 at the same time?

This:

RewriteRule ^about/$ /test/about.php [L,E=CLEAN_CONTACT_URL:1]
RewriteCond %{ENV:REDIRECT_CLEAN_CONTACT_URL} !1
RewriteRule ^about.php$ /test/about/ [R=301,L]


works exactly the way that I wanted, I just didn't know how to use RewriteCond to do something like this. Now I'm glad I posted my question!

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Vandalay111

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

RewriteRule ^about/$ /test/about.php [L]
RewriteRule ^about.php$ /test/about/ [R,L]


This is probably just your example, but this shouldn't actually result in a rewrite loop because of the additional /test subdirectory in the substitution. (/test/about.php won't match the pattern ^about.php$)

But anyway, assuming you want to (internally) rewrite /about/ to /about.php and (externally) redirect /about.php back to /about/ (user friendly URL), then your solution can be optimised. There is no need to set an environment variable. (Note also, that you only need to do the external redirection if you are changing an existing URL structure. If this is a new site and the "unfriendly" URLs have not been indexed or exposed, then the external redirection is not necessary.)

Generally, external redirections should come before any internal rewrites. The rewrite loop can be avoided by checking against %{THE_REQUEST}, which contains the initial HTTP request. (so, no need for the environment variable to remember state.) So this can be rewritten as:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /about.php
RewriteRule ^about.php$ /about/ [R=301,L]

RewriteRule ^about/$ /about.php [L]


As mentioned, %{THE_REQUEST} contains the initial HTTP request (which does not change), which is of the form: GET /about.php HTTP/1.1

It's also good practise to separate rule sets with a blank line once you start using RewriteCond directives to make it clear (more readable) that the RewriteCond directive only applies to the single RewriteRule directive that follows.

Checking for about.php in the RewriteRule (as well as RewriteCond) might seem unnecessary at first, since the RewriteCond directive is what actually prevents the rewrite loop, but this is actually required for efficiency. Since the RewriteRule is tested first, this ensures that only relevant requests are processed.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme