1

Sorry if this question has been asked a ton however, I couldn't find a solution based off previous questions asked on this site and google.

I have this URL http://mywebsite.com/index.php?action=users&user=John to generate a profile for a user. I would like to clean this URL so I can simply type http://mywebsite.com/users/John or any action. Now, to go to a specific action, I can simply use:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /


RewriteRule ^([a-zA-Z0-9\-]+)$ index.php?action=$1 [L]

then visit http://mywebsite.com/users to visit the users page but to access a specific user I have &users=John. I thought I could simply use:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /


RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?action=$1&$2=$3

to make my URL work like so http://mywebsite.com/action/extra_action. Confusing? Without cleaning the url I would have http://mywebsite.com/index.php?action=ACTION&OTHER=. Some actions don't require &other= and would lead to a different page if you add on to it. For example, /index.php?action=users leads to a page where a list is generated; index.php?action=users&user=John leads to a dynamic profile of John. I would like to make the URL http://mywebsite.com/users or if they want to visit a profile /users/John. Thanks for reading!

1 Answers1

0

You need to make the / and the last two parameters optional, otherwise the redirects will not work unless they have two /.

RewriteRule ^([a-zA-Z0-9]+)(/?)([a-zA-Z0-9]+)?(/?)([a-zA-Z0-9]+)?$ index.php?action=$1&$3=$5
  • Can I have multiple RewriteRules for each situation? – somethinghereplease1 Jan 29 '14 at 22:59
  • @davidxd33 Sorry, I misunderstood you the first time. See edit. –  Jan 29 '14 at 23:13
  • No problem, this works well expect for when adding the extra parameter it doesn't go to the correct page. `site.com/users` works but `site.com/users/John` redirects to `/users`. It could be how I serve the pages causing this. On users.php, I check to see if `$_GET['user']` isset then I serve the dynamic profile. If it isn't set, I render the users page. Could that be the problem? Thanks for helping with with this. I understand the logic of that rule though. It looks like it should work. – somethinghereplease1 Jan 30 '14 at 13:40
  • One more problem I discovered, when accessing a directory, `site.com/directory` it redirects to `site.com/directory?action=directory` – somethinghereplease1 Jan 30 '14 at 13:46