0

I have a couple of rules in my .htaccess file in order to make the URLs a bit cleaner, however, they seem to be cancelling each other out.

The first rule is just to remove the .php from page names,

example : mysite.com/join rather than mysite.com/join.php

RewriteEngine On
Rewrite Condition : %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

The second rule is to make it easier for Users to share their profiles on my site,

example : mysite.com/user1 rather than the actual URL mysite.com/profile.php?user=user1

RewriteRule ^([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]

I've been playing round with them, and they essentially cancel each other out - Any ideas on how I can get them both working?

Thanks

Nick
  • 138,499
  • 22
  • 57
  • 95
Rrossco
  • 17
  • 3

1 Answers1

1

The problem you have is that both conditions are almost identical i.e. anything that ^([_A-Z0-9a-z-+]+)$ matches will also be matched by ^([^.]+)$, so someone accessing mysite.com/user1 will get redirected to mysite.com/user1.php since that is the first rule that is encountered, and it has the L flag to prevent processing more rules. To prevent this happening you need to make the rules different, e.g. perhaps require user pages to be mysite.com/users/user1? Then you could write the rules as

RewriteRule ^([^./]+)$ $1.php [NC,L]
RewriteRule ^users/([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]

Note that you need to add / to the characters not to be matched in the first rule, otherwise it will still match mysite.com/users/user1.

Edit

A couple of other alternatives:

If you were willing to have actions (e.g. join) use URLs such as mysite.com/action/join then you could keep users at the top level e.g.

RewriteRule ^action/([^.]+)$ $1.php [NC,L]
RewriteRule ^([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]

Or if you know the names of all your actions you could put them in an alternation (this would require that you couldn't have a user called e.g. join):

RewriteRule ^(join|login|logout|delete)$ $1.php [NC,L]
RewriteRule ^([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]
Nick
  • 138,499
  • 22
  • 57
  • 95
  • Thanks for your answer Nick - I see the issue now, however do need both ... And basically need the profile pages to be just the username, rather than appending a number to the end, as it'll be for them sharing their profiles on social media, etc. Hopefully I'll be able to work it out / someone could help further? – Rrossco Aug 27 '18 at 09:27
  • Not sure what you mean by `appending a number to the end`, I was just suggesting it would be `mysite.com/users/Rrossco` for example. Anyway, I've offered up another couple of alternatives that might work for you in my edit. – Nick Aug 27 '18 at 09:54
  • Ah amazing, thanks Nick - Sorry I misinterpreted your first comment. Your second option works PERFECTLY. I only have a couple of pages that I / my users would link to directly ie) login, join, etc. so for now this is exactly what I need! I can obviously add more pages into that list if needs be, but it's extremely unlikely anyone will create an account with those names as they are businesses and tradesmen. Thanks again - I understand these rules a lot better now. R – Rrossco Aug 27 '18 at 10:04