0

I am trying to set common rule in htaccess for removing .php extension and links using different query strings like

domainname.com/welcome.php?user=normal&type=free&uid=100
domainname.com/welcome.php?user=normal
domainname.com/welcome.php?log=new&theme=red

And expected links like

domainname.com/welcome?user=normal&type=free&uid=100
domainname.com/welcome?user=normal
domainname.com/welcome?log=new&theme=red

Rule set in htaccess file like:

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/welcome/$ $1/welcome.php? [QSA,NC,L]

But some how not working. Am I doing something wrong.

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
rock
  • 25
  • 1
  • 4

2 Answers2

1

Try this in the .htaccess file at root directory:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  !welcome\.php [NC]
RewriteRule ^welcome/?  /welcome.php      [NC,L]

The query string will be passed automatically.

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
  • Thanks for reply. I am new to writing htaccess rules. what if I pass link like domainname.com/welcome?user=normal&type=free&uid=100 and in htaccess file set rule like: RewriteCond %{REQUEST_URI} !welcome\/? [NC] RewriteRule ^(.*)/welcome/?$ $1/welcome.php?%1 [NC,L] – rock Apr 04 '13 at 18:48
  • @Resh: Seems to me that you are, in fact, doing it correctly. However, the appended `?` on the end of the rule means 'query string discard`, an alternative for the `QSD` flag. @faa's code is correct. – Mike Rockétt Apr 04 '13 at 19:09
  • @Resh Did you try my answer? As explained, the query will be passed automatically. You don't need the QSA flag because the rule is not creating a new query, just using the incoming one. On the other hand, you have `^(.*)/welcome..` in the regex, but the URL directory structure only has one level. That's an error unless the requests are not as described. Just copy+paste the code in my answer to the .htaccess file and give it a try. – Felipe Alameda A Apr 04 '13 at 19:19
0

Your rule sets in your answer and in your comment are disordered. If you want /$dynamic to be rewritten into /$dynamic.php with a query string, you must try this code:

Options+ FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1.php [QSA]

Even /index1, /index2, /index3, /etc will be rewritten.