Search code examples
.htaccessurl-rewritinghttp-redirect

.htaccess RewriteRule for navigation


I need to redirect

www.monsite.fr/css

to

www.monsite.fr/index.php?p=css

in .Htaccess

I tried lots of answers found on the site without success like this one: .htaccess RewriteRule for directory

I tried with on an other site to make regex, but once tested it doesn't work

exemples:

RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
RewriteRule \/([^\/]*)$ /index.php?p=$1 [L]
RewriteRule ^([^.]+?)/?$ /index.php?p=$1 [L,QSA]

but none works.

I used a site that tells me these rules do what I want :

RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]

but finally I was redirect to

www.monsite.fr/css/?p=css

what 's wrong?


Solution

  • RewriteEngine On
    RewriteBase /
    #if file exist, serve the file
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,NC,QSA]
    RewriteRule ^([^/]+)/([^/]+)/?$ index.php?p=$1&n=$2 [L,NC,QSA]
    

    this solution seems to work. it is necessary to put the rule which checks the existence of the file before the redirection so that the request towards the css file is not redirected.