0

I want to rewrite URLs like this:

http://domain.com/category.php?id=xx

to:

http://domain.com/category/?id=xx

Also, I want to hide the index.php. How can I achieve this with .htaccess? The following code doesn't work:

##REDIRECTS-START

##REDIRECTS-END

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(ADMIN.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
### ROOT DIR TO PROJECT
RewriteRule . wallkill/index.php [L]

Thanks.

Amar Syla
  • 3,523
  • 3
  • 29
  • 69

2 Answers2

1

Try this code

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

reference link

Community
  • 1
  • 1
avinash pandey
  • 1,321
  • 2
  • 11
  • 15
  • This breaks if the request is `/category/` (note the trailing slash). Will cause a 500 server error. – Jon Lin Mar 31 '15 at 19:17
  • Almost solved my problem. When I visit http://domain.com/category?id=xx, it works fine, but when adding a slash after category, like this: http://domain.com/category/?id=xx it fails. Is there any possibility to cover this one too? – Amar Syla Mar 31 '15 at 19:18
  • @JonLin Exactly, I'm getting a 500 internal server error. Do you have a solution for that? – Amar Syla Mar 31 '15 at 19:18
0

You need 2 sets of rules. The first set redirects the browser to requests without the .php at the end, then internally rewrites the .php back. The second rule redirects the browser to requests without the index.php.

So something like:

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /+(.*)index\.php
RewriteRule ^ /%1 [L,R]

RewriteCond %{THE_REQUEST} \ /+([^\?]+)\.php
RewriteRule ^ /%1/ [L,R]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

Note that when you add that / trailing slash to the end, your relative URL base changes, and all your relative links will break. To fix this, either make sure all your links are absolute (start with / or http://) or add a relative base to the header of all your pages:

<base href="/" />
Jon Lin
  • 142,182
  • 29
  • 220
  • 220