skip to Main Content

I have a maybe simple Problem. I have serveral URLs that needed to be redirected in this way:

if the URL "/abc/" is called, it should show the content located under "xyz.html"

i can do that with

RewriteRule abc$ xyz.html 

but "xyz.html" should be 301 redirecting to "/abc" if it is called.

This is my simple problem i am searching for an solution since hours.

it would be easy if its like "test.html" and /test/. i can do it like

RewriteRule ^(.+).html$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*.html$ %{REQUEST_URI}.html [END]

But in my case i have a bunch of URLs with no pattern.

All i tried results to server misconfiguration.
Can you help me with that one sample?

thanks, kanuddel

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for the great Explanation!

    I tried it with a second URL, where "/xxx/" should show "zzz.html" But this gave me an Misonfiguration. I tried to shorten it like this:

    RewriteEngine ON
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{REQUEST_URI} ^/abc/? [NC]
    RewriteRule ^(.*) /xyz.html [L]
    RewriteCond %{REQUEST_URI} ^/xxx/? [NC]
    RewriteRule ^(.*) /zzz.html [L]
    
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{REQUEST_URI} ^/xyz.html/? [NC]
    RewriteRule ^(.*) /abc/? [R=301,L]
    RewriteCond %{REQUEST_URI} ^/zzz.html/? [NC]
    RewriteRule ^(.*) /xxx/? [R=301,L]
    

  2. Could you please try following.

    RewriteEngine ON
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{REQUEST_URI} ^/abc/? [NC]
    RewriteRule ^(.*) /xyz.html [L]
    
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{REQUEST_URI} ^/xyz.html/? [NC]
    RewriteRule ^(.*) /abc/? [R=301,L]
    

    Detailed Explanation:

    Why there is redirection loop with normal rule writing: Question from OP is interesting and contradictory(kind of), why because lets say we write 1st rule to redirect(in backend) from abc url to xyz.html it will work fine. But then when we write our 2nd rule which is to rewrite url from xyz.html to abc/ which is exactly opposite of first rule. Hence it becomes a loop, ASAP 1st rules gets served it reaches out to 2nd rule and it serves it back to 1st rule thus a REDIRECTION INFINITE LOOP(till 50 cycles or so).

    How to prevent redirection loop?

    • First since both rules are exactly opposite of each other so in a normal rule writing it will become a loop, now how could we prevent it to become a loop? Answer is using: RewriteCond(explained in next step)

    • I have used an ENVIRONMENT VARIABLE named ENV:REDIRECT_STATUS which will have any redirection status in its value. 2 things we need to understand about this variable.

      1- Without any request redirection its default value is NULL.

      2- Whenever we do a successful redirection its value becomes 200(NON ZERO), which we can make use of in our conditions part.

    Detailed explanation of htaccess Rules:

    • Now coming to the condition explanation part: In RewriteRuleRewriteRule ^(.*) /xyz.html [L] I have NOT done any rewriting of url on browser why because of the THUMB RULE that we always want to show USER FRIENDLY URLs to users, so environment variable ENV:REDIRECT_STATUS will always be ZERO here.

    • Coming to 2nd RewriteRule now RewriteRule ^(.*) /abc/? [R=301,L] where user already using a user NON-friendly URL so first thing is I need to rewrite URL in browser to user friendly url hence R=301(redirection with permanent flag is used here). Once Redirection happens through this condition, 1st condition will start failing now why because that checks if REDIRECT_STATUS variable is NULL which is NOT after serving redirection from 2nd condition. Hence this is how it prevents loop by this small trick 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search