mittelmania
mittelmania

Reputation: 3561

Use mod_rewrite to rewrite get arguments with slashes

I have a website which has a few standalone webpages (like signup.php) and a main PHP file that displays the rest of the pages using a GET parameter.

I wish to rewrite HTTP requests to the server so that a request like:

http://example.com/website/settings/ would display the page

http://example.com/website/index.php?page=settings, without affecting all the other pages.

I'm quite new to mod_rewrite and all of my attempts have resulted in either a 404 or 500 response code. What is the correct way to write the RewriteCond and RewriteRule?

Upvotes: 0

Views: 763

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Add these rules in the htaccess file in the document root (where the website folder is in):

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /+website/(?:index\.php|)\?page=([^&]+)/(&|\ |$)
RewriteRule ^ /website/%1/ [L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^website/(.+)/$ /website/index.php?page=$1 [L,QSA]

Or you could instead put these rules in the website folder if that makes more sense:

RewriteEngine On
RewriteBase /website/

RewriteCond %{THE_REQUEST} \ /+website/(?:index\.php|)\?page=([^&]+)/(&|\ |$)
RewriteRule ^ %1/ [L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ index.php?page=$1 [L,QSA]

Upvotes: 1

Related Questions