1

I have...

| .htaccess : (v1)

RewriteEngine on

RewriteRule ^in?$ login.php

So, /in --is-really--> /login.php

This much works great. We all can learn how to do this from: .htaccess redirect with alias url

But, I want it to also work in reverse...

If someone should enter /login.php into the address bar, I want it to change to /in.

So also, /login.php --rewrites-to--> /in

From this Answer to a different Question, I want to be ready for anything, using REQUEST_URI. So, my .htaccess file starts with this...

| .htaccess : (v2)

RewriteEngine on

# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]

# "in" --> login.php
RewriteRule ^in?$ login.php

That also works great.

But now, I want to add this rule (my Question here) for /in <--> /login.php both ways, just how / <--> /index.php already works with .htaccess (v2). So, I adopted the settings and added a second rule...

| .htaccess : (v3) —not working!

RewriteEngine on

# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]

# "in" --> login.php, and also redirect back to it
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php
RewriteRule (^|/)login\.php(/|$) /%1in [R=302,L]
RewriteRule ^in?$ login.php

...but then /in and /login.php both cause an infinite redirect loop.

What's the right way to do this, still using REQUEST_URI, and still having both rewrite rules (for index.php and for login.php)?


These Questions did not help:

Jesse
  • 750
  • 1
  • 9
  • 25
  • `but that causes an infinite redirect loop`: Did you try testing in a new browser or from command line `curl`? – anubhava May 19 '22 at 09:39
  • @anubhava Tested many browsers. And, even logically it makes sense, because the code as it does this: Rule 1. `login.php` --> `in`; Rule 2. `in` = `login.php`. So, of course it will infinitely redirect. Is there a `RewriteAlias` option or such way to do this "properly"? – Jesse May 19 '22 at 12:56
  • 1
    `of course it will infinitely redirect.` No it won't because after first rewrite to `/login.php` variable `REDIRECT_STATUS` will become `200` and then `RewriteCond %{ENV:REDIRECT_STATUS} ^$` will stop redirect – anubhava May 19 '22 at 13:34
  • 1
    At the gym, I will run some tests based on this later, finding what I overlooked. Good info. – Jesse May 19 '22 at 13:59
  • @anubhava I found that it was other settings that were messing me up. You were right on. I will update my Question so that it includes the full settings file. If you want, you can write the obvious Answer. – Jesse May 19 '22 at 16:14

2 Answers2

1

Reason of redirect loop is a missing RewriteCond %{ENV:REDIRECT_STATUS} ^$ before first redirect rule that removes index.php. Remember that RewriteCond is applicable to immediate next RewriteRule only.

Suggested .htaccess:

RewriteEngine on

# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php$ [NC]
RewriteRule ^ /%1 [R=301,L]

# "in" --> login.php, and also redirect back to it
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php$ [NC]
RewriteRule ^ /%1in [R=302,L]

RewriteRule ^in?$ login.php [L,NC]

It won't cause redirect loop because after first rewrite to /login.php, variable REDIRECT_STATUS will become 200 and then the RewriteCond %{ENV:REDIRECT_STATUS} ^$ will stop redirect looping.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Sorry, I made a mistake, but fixed it, for `.htaccess` (v3) The "index.php" group wrongly had "login.php" in it. It was trying to have BOTH rules, but the second without the `ENV:REDIRECT_STATUS` setting that made the loop. Could you kindly add that *`# Remove index.php...`* group to the answer so it has both rules? Not to boss, it was my mistake with v3. tysm. – Jesse May 19 '22 at 16:37
  • 1
    Sure thing, it is updated now. – anubhava May 19 '22 at 16:46
  • 1
    That's it! This saves me. So surprised this is actually a new Question. 1,000 Beremie points. – Jesse May 19 '22 at 17:57
0

Thanks to the help from the user with the correct answer, I found that...

RewriteCond %{ENV:REDIRECT_STATUS} ^$

...doesn't go in .htaccess only once, but every time on the line before...

RewriteCond %{REQUEST_URI} ...
Jesse
  • 750
  • 1
  • 9
  • 25