views:

33

answers:

2

How can I allow visitors to use this link (www.example.com/news?id=34) in order to see (www.example.com/index.php?page=news&id=34)

Right now I am hiding the extensions of my PHP files in order to make the links look nice. When visitors go to (www.example.com/news) they can see the page (www.example.com/index.php?page=news). However, when they go to (www.example.com/news?id=12) they get a 404 error.

At this point, PHP does not recognize the id parameter. Here is what I currently have in my .htaccess file

Options +FollowSymlinks
RewriteEngine on

# catch request with no querystring like /Home
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

# make other requests with a non-empty query string go to /index.php?id=XXXXX
RewriteCond %{QUERY_STRING} ^.*$
RewriteRule ^$ /index.php?id=$1 [QSA,L]
A: 

The condition of your first rule fails as the query is not empty. Try it without that condition but with the following condition instead:

RewriteCond $1 !=index.php
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
Gumbo
A: 

Your second test pattern (^$) only matches the case where the user doesn't put any path information in, but does include a query string (e.g. www.example.com/?id=12). Also note that the backreference $1 has no value in that rule either.

The simplest solution is to just combine the two rules, since you can use the QSA flag to do the work of appending the id=# part for you:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?page=$1 [QSA,L]
Tim Stone
I really really really really really appreciate your help. You totally saved my day.
Eyad A
@Eyad: Glad to help!
Tim Stone