Martin
Martin

Reputation: 79

Rewrite URL with .htaccess for 2 parameters

How can I convert URL from:
http://example.com/site/delete-page.php?site=first&page=second

To:
http://example.com/site/first/delete-page/second

Thank You!

Upvotes: 1

Views: 2609

Answers (1)

arkascha
arkascha

Reputation: 42885

This is the normal rule for that specific task:

RewriteEngine on
RewriteRule ^/site/([^/]+)/delete-page/(.+)$ /site/delete-page.php?site=$1&page=$2 [L]

To use the same rule inside a .htaccess style file you have to modify it slightly: remove the leading slash (/) from pattern and target:

RewriteEngine on
RewriteRule ^site/([^/]+)/delete-page/(.+)$ site/delete-page.php?site=$1&page=$2 [L]

This assumes that the .htaccess style file is located inside the folder holding the site folder. You can also place the file inside that site folder, then obviously you have to remove the site/ part from the rules pattern and target. Also you have to take care that the interpretation of .htaccess style files is enabled at all for that host and location.


In general you should always prefer the first version and place such rules inside your http servers host configuration. .htaccess style files are notoriously error prone, hard to debug and they really slow the server down. They are only offered as a last option for situations where you do not have access to the server configuration. For example when using a really cheap shared web hoster service...

Upvotes: 3

Related Questions