0

Yes, I know this has been asked hundreds of times. But I just can't find a version that actually works. Currently, all the ones so far I've tried currently work in removing .php, but that is when the problems begin. A few pages on my site depend on $_GET requests, e.g profile So when you go to the url http://www.example.com/profile?username=System you will get a 404, while without the ?username=System it works fine. The current code I have is

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
ChocCookieRaider
  • 135
  • 1
  • 2
  • 8
  • have you got your AllowOrverride set to All? in xammp and http-conf? Also put Options Indexes FollowSymLinks Includes ExecCGI in your http.conf – Rachel Gallen Jan 15 '13 at 19:38
  • This is being hosted on a remote server, so I don't know, but before I decided to change the page system, rewrite like this worked fine. But then pages were done by index.php?page=profile&username=System – ChocCookieRaider Jan 15 '13 at 19:46
  • You are getting a 404 error because the conditions in the rule set don't match and obviously there is no folder `http://www.example.com/profile?username=System` But, what script is supposed to get the parameters passed in the incoming URL. i.e `username=System`, the script with the name `profile` (profile.php) or another one not in the URL, like `index.php`? – Felipe Alameda A Jan 15 '13 at 19:58
  • If going to the page `profile?username=System`, the parameters would be passed to `profile.php` while if going to `search?query=hi+there` the parameters would be passed to `search.php`. – ChocCookieRaider Jan 15 '13 at 20:05
  • Try changing the second rewrite condition to `RewriteCond %{REQUEST_FILENAME} !-f` – Supericy Jan 15 '13 at 20:14

2 Answers2

2

You need [QSA] (Query String Attached) at the end of your RewriteRule.

RewriteRule ^(.*)$ $1.php [QSA]
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
1

You may try this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING}  .       [NC]
RewriteCond %{REQUEST_URI}  ^/([^/]+) [NC]
RewriteRule .*   %1.php               [L]

Will map silently this

http://www.example.com/ScriptName?key1=val1

To:

http://www.example.com/ScriptName.php?key1=val1

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
  • Works perfectly, except for one small problem. When a page is being accessed without a query, you get a 404 error. – ChocCookieRaider Jan 15 '13 at 20:41
  • That's the expected behavior, hence `RewriteCond %{QUERY_STRING} . [NC]`, because I assumed the page `http://www.example.com/ScriptName`, for example, does not exist. However, if you still want to map that page to the script with it's name, without a query, just remove that line from the set of rules. – Felipe Alameda A Jan 15 '13 at 20:49