htaccess URL rewrite - requested URL not found on server

微笑、不失礼 提交于 2019-12-24 06:47:44

问题


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!


回答1:


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]



回答2:


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




回答3:


Don't know what the "[A-Z]{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)?



来源:https://stackoverflow.com/questions/8766231/htaccess-url-rewrite-requested-url-not-found-on-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!