Jexon Jexon - 6 months ago 11
PHP Question

Rewrite URL to hide ?get= in .htaccess

This is the structure of my website:

website.com/events/beachparty?date=1224
website.com/events/poolparty?date=0101
website.com/events/boatparty?date=1105


There are lots of different pages all of which I would like to use the get feature on.

I want to rewrite the URL using htaccess so that they can be loaded like this:

website.com/events/beachparty/1224
website.com/events/poolparty/0101
website.com/events/boatparty/1105


Is this possible without having to create a separate rule for each page?

Thanks in advance!

Answer

Is this possible without having to create a separate rule for each page?

Yes, you can use a regex, i.e.:

RewriteEngine On
RewriteRule ^events/page([0-9]+)/([0-9]+)/?$ /events/page$1?date=$2 [L]

The above will rewrite:

www.yoursite.com/events/page99/123

to

www.yoursite.com/events/page99?date=123

Notes:

  1. ([0-9]+) will match 1 or more (+) digits
  2. The last forward slash is optional /?
  3. $ means the end of the line (url)
  4. [L] = Last, apache will stop processing further rules

Update based on your comments:

RewriteEngine On
RewriteRule ^events/([0-9a-zA-Z]+)/([0-9]+)/?$ /events/$1.php?date=$2 [L]
RewriteRule ^events/([0-9a-zA-Z]+)/?$ /events/$1.php [L]

[0-9a-zA-Z] - will match any digit or letter from a to z or A to Z, 1 or more times

Comments