简体   繁体   中英

htaccess URL rewrite - requested URL not found on server

Having an issue where the URL rewrite is running but the server will not display the contents. My .htaccess file is

    RewriteEngine On
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project\.php [NC]
    RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
    RewriteRule . project-%1.html? [R=301]

Thanks for your help!

EDIT: Sorry, here is some more info!

I want to rewrite

    www.siteurl.co.uk/project.php?id=82

to

    www.siteurl.co.uk/project-82.html

The code in the .htaccess rewrites the URL perfectly but the page does not display. I get a

    404 The requested URL /project-82.html was not found on this server. 

I hope that helps!

You only have one half of the solution, creating the pretty urls. What is missing is the other half, sending the pretty urls to an application for processing as below

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project\.php [NC]
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule . project-%1.html? [R=301]

#you need this rule to process your www.siteurl.co.uk/project-82.html request
RewriteRule ^project-([0-9]+)\.html$ project.php?id=$1 [L,NC,QSA]

try putting RewriteRule ^project-%1.html? [R=301] RewriteRule ^project-%1.html? [R=301] instead of the .

Don't know what the "[AZ]{3-9}\\ " is for? maybe I'm missing something?

But maybe something like this instead:

RewriteCond %{THE_REQUEST} ^project\.php [NC]
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule . project-%1.html? [R=301]

or

RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^/project\.php project-%1.html? [R=301]

Note that this is just off the top of my head, and I didn't test it.

EDIT: Just adjusted the second example which had an extra slash in it, and tested it out on my webserver (note that I'm using IIS/URLRewrite, but should be same):

RewriteBase /
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^project\.php project-%1.html? [R=301]

I entered http://localhost/project.php?id=123 and it went to http://localhost/project-123.html

But I think I missed something... you say the url rewrite is working, and the url is changing but you're getting a 404? In that case then your rewrite sounds fine - and something else is not permitting your html files to be accessed (or they don't exist)?

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