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.
-
\$\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– atdc2015年07月11日 05:57:24 +00:00Commented Jul 11, 2015 at 5:57
2 Answers 2
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.
-
\$\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– atdc2015年07月11日 06:20:17 +00:00Commented Jul 11, 2015 at 6:20
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.
-
\$\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– atdc2015年07月11日 09:35:49 +00:00Commented Jul 11, 2015 at 9:35