5
\$\begingroup\$

My htaccess has this block in it:

RewriteRule ^login$ _user/login.php
RewriteRule ^login\.php$ _user/login.php
RewriteRule ^_user/login$ _user/login.php
RewriteRule ^_user/login\.php$ _user/login.php

How do I consolidate this block? The objective is to point to _user/login.php regardless of where on the server it's called from.

\$\endgroup\$
1
  • \$\begingroup\$ My only other rules are similar to this aside from a filesmatch and the options. For now I just know I could be doing this better but am a fool when it comes to regex and don't fully understand the syntax of htaccess. \$\endgroup\$
    – atdc
    Jul 11, 2015 at 5:57

2 Answers 2

4
\$\begingroup\$

The last rule is completely superfluous, but harmless: it just maps the desired URL to itself.

The four cases can be combined into one rule:

RewriteRule ^(_user/)?login(\.php)?$ _user/login.php

In a regular expression, (something)? makes the something optional. That is, whether the URL starts with _user/ or not, and whether the URL ends with .php or not, it will get mapped to the desired login page.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you! I was trying other things earlier with (.*) but never knew about ? This answer works perfectly but leads me to my next question. I forgot that I always wanted the url to display site/login and hide the subdirectory. Is that just a complicated rewriterule? Sometimes I feel like I'm going in the wrong direction. \$\endgroup\$
    – atdc
    Jul 11, 2015 at 6:20
2
\$\begingroup\$

Some remarks about the bigger picture:

What is your goal with this? Why are you mapping four differnt urls to the same resource?

It it generally a bad idea to have multiple URLs display the same content. (Google "duplicate content").

Instead you should have all those additional URLs redirect to one canonical URL - if you actually need those additional URLs. You should design your site, so that only one URL is necessary.

\$\endgroup\$
1
  • \$\begingroup\$ I have the login file called from two different files. In two different folders. I'll look into canonical URLs. Only one url is necessary, I more or less just want to direct "login" to the login page. [there's no REAL goal] \$\endgroup\$
    – atdc
    Jul 11, 2015 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.