Warm tip: This article is reproduced from serverfault.com, please click

.htaccess rewrite hidden parameter and keep files the same

发布于 2020-11-29 12:09:15

I am working with .htaccess using apache.

I want be able to type in a URL like this:

example.com/bob

and I want to rewrite the URL as

example.com/index.php?c=bob

I also don't want it to affect real files and to load them normally

example.com/account.php

In addition to all of this, I want to rewrite real files without the .php

example.com/account

So far I've been focusing on the index.php?c=bob task. I have this so far in my .htaccess

RewriteCond $1 !^(index\.php|account\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php?c=$1 [L]

The work heavy solution I thought of is to list all of my files in line 1 above, I am looking for a more efficient way

Questioner
Jake
Viewed
0
RavinderSingh13 2020-11-30 01:47:21

Could you please try following.

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ index.php?c=$1 [L]