简体   繁体   中英

.htaccess rewrite rule does not work

I am having some trouble with the Rewrite rule in .htaccess and none of the other answers on here seem to do the trick!

Basically the code below works

RewriteRule ^index.php$ test.php

However as soon as I add anything else it does not! The two lines of code below do not work at all

RewriteRule ^project-([0-9]+)\.html$ project.php?id=$1
RewriteRule ^project.php?id=82$ test.php

When I get this working I eventually want to take the project title from the page and insert it in the url (just like this site if you take a look in the URL) AND turn all .php to .html but I have seem to have fallen at the first hurdle!

Any ideas anyone? Any help will be gratefully received!

See the following edits to your code below

#escape the . so it does not match indexsphp etc
#added [NC,L] so it is not case sensitive and last rule processed
RewriteRule ^index\.php$ test.php [NC,L]

#added [NC,L] so it is not case sensitive and last rule processed 
# and QSA to pass through any existing query string  values
RewriteRule ^project-([0-9]+)\.html$ project.php?id=$1 [L,NC,QSA]

If this rule

RewriteRule ^project.php?id=82$ test.php

is intended to match only when the id=82, then it will not work and needs to be re-written as follows

#if the query string has an id=82
RewriteCond %{QUERY_STRING} id=82 [NC]
#and resource is project.php, send it to test.php
RewriteRule ^project\.php$ test.php [NC,L]

EDIT

I would like to take project.php?id=82 and change to project-82.html

See rules below

#if the original request has project.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project\.php [NC]
#capture the id param
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
#and 301 redirect to project-id.html
RewriteRule . project-%1.html? [L,R=301]

Since you say the index.html rewrite works fine I guess you already have this, but instead of assuming I suggest still that you make sure that rewrites are actually enabled. I always put this first in my .htaccess files:

RewriteEngine On

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM