3

I have a problem with redirect URL in .htaccess. I want to remove .php & question marks from the URL.

For Example: www.example.com/test.php?id=12 to www.example.com/test/12 need like this format.

I tried using this code in my .htaccess

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

This code just removes the .php extension from the URL. Also need to remove the question mark.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Prabhu
  • 1,127
  • 1
  • 13
  • 30

2 Answers2

6

That code appears to be from one of my answers :)

Replace your code with this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

## hide .php extension snippet

# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?id=([^&\s]+) [NC]
RewriteRule ^ %1/%2? [R,L]

# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?id=$2 [L,QSA]

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I used this code to remove the .php & ? from url. It works for me. But it doesn't load the css for the page. Any idea? – Prabhu Jul 31 '13 at 07:23
  • For css, js, images make sure to use absolute paths e.g starting with `/` or `http://` – anubhava Jul 31 '13 at 08:01
  • ok. It works. If i redirect from that page, redirection cond should add like this www.example.com/test/index.php. And if this type of url "www.example.com/example.php" I want like this "www.example.com/example". Help me to solve this issue – Prabhu Jul 31 '13 at 09:06
  • 1
    That's not correct. `/test.php` is becoming `/test` with this code. – anubhava Jul 31 '13 at 09:20
  • ok. Its correct. www.example.com/test.php?id=12 to www.example.com/test/12. This one is perfect. After that in this page "www.example.cpm/test/12" every link is changed like "www.example.com/index.php" to "www.example.com/test/index.php". – Prabhu Jul 31 '13 at 09:25
  • Its the same problem, you have relative hrefs in your page. Make sure to use absolute paths for your links e.g starting with `/` or `http://` – anubhava Jul 31 '13 at 09:26
  • I have one query. If the url is like this www.example.com/test.php?id=12&action=t the above re direction condition should throw the sql error. Can you help me to solve this? – Prabhu Aug 02 '13 at 06:31
  • 1st rule will above make it `/test/12` and that will be internally forwarded to `/test.php?id=12` Pls note that `(&action=t)` will be stripped from redirected URL. – anubhava Aug 02 '13 at 06:40
  • I have an another issue with this rewrite url. If i used post method for submission the value should be '0'. any idea to fix this? – Prabhu Aug 02 '13 at 11:30
  • I didn't understand. What value should be zero? Please provide full details like what is starting URL and what result you expect. – anubhava Aug 02 '13 at 11:50
  • Value means, Form post method values will changed to 0. I need the correct value – Prabhu Aug 02 '13 at 11:55
  • You still didn't provide me any details like what is starting URL and what result you expect. – anubhava Aug 02 '13 at 11:58
  • This is the start url www.example.com/test.php.In this page have a contact form with many fields. All the values are submit by post method. I cant get any values. All the values will show as Zero. – Prabhu Aug 02 '13 at 12:01
  • Ok got it, POST data is not transferred after redirection. You need to skip redirect rule when POST data is present. – anubhava Aug 02 '13 at 12:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34692/discussion-between-shivaji-and-anubhava) – Prabhu Aug 02 '13 at 12:44
  • kudos @anubhava for his civility and helpfulness! – dayuloli Mar 03 '14 at 07:49
0

How about this? It requires mod_rewrite.

RewriteEngine On
RewriteRule ^test\/([0-9]+)$ test.php?id=$1
Austin Brunkhorst
  • 20,704
  • 6
  • 47
  • 61