Miss A
Miss A

Reputation: 5

Clean url for static and dynamic pages

I have a website with a mixture of static pages without variables (ex: about.php) and some dynamic pages (ex: searchresults.php?a=1&b=2)

Right now my .htaccess allows for the static pages to show, but not the dynamic ones. Everything I've tried to do to make the dynamic pages work, however, breaks the clean URLs for the static pages.

RewriteEngine On

DirectoryIndex index.php index.html

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

I would like the final result to look like this:

http://example.com/about.php -> http://example.com/about

and

http://example.com/searchresults.php?a=1&b=2 -> http://example.com/searchresults/1/2

Is what I'm looking to do possible?

Upvotes: 0

Views: 1291

Answers (2)

Nilesh Chourasia
Nilesh Chourasia

Reputation: 408

Try below line of code for your htaccess file.

RewriteEngine on
RewriteRule aboutus/$               about.php   [NC,L,QSA]
RewriteRule searchresults/(.*)/(.*)/$           searchresults.php?a=$1 & b=$2   [NC,L,QSA]

Upvotes: 0

Mike Rockétt
Mike Rockétt

Reputation: 9007

Yes, it's possible.

Firstly, you'll need to change your rule to strip the extension. Currently, you have two rules that match anything (meaning that the second will never trigger), and the second rule doesn't have a condition.

Secondly, your search rule will need to be specified explicitly.

Your .htaccess file should now look like this:

RewriteEngine on

# Rewrite the search page

RewriteRule ^searchresults/([^/]+)/([^/]+)/?$ searchresults.php?a=$1&b=$2 [QSA,L]

# Allow requests without php/html extensions

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ $1.html [L]

Upvotes: 1

Related Questions