简体   繁体   English

mod_rewrite友好URL与404冲突

[英]mod_rewrite Friendly URL clashing with 404

I've been struggling with my mod_rewrite for a while now and I thought I'd cracked it… 我一直在挣扎我的mod_rewrite一段时间了,我以为我已经破解了……

My CMS generates blog post URLs with post.php?s= and I've been trying to remove this part of the URL so that they're nice and user friendly. 我的CMS使用post.php?s =生成博客文章URL,而我一直在尝试删除URL的这一部分,以便它们美观且用户友好。 Here's my mod_rewrite: 这是我的mod_rewrite:

<ifModule mod_rewrite.c>

DirectoryIndex index.php index.html

ErrorDocument 404 http://tempertemper.net/error.php

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

RewriteCond %{HTTP_HOST} ^www.tempertemper\.net$
RewriteCond %{REQUEST_URI} !cron.php
RewriteRule ^(.*)$ http://tempertemper.net/$1 [R=301,L]

RewriteCond %{QUERY_STRING} ^s=(.+)$
RewriteRule post.php /%1? [R=301,L]

RewriteRule ^resources /archive? [R=301,L]

RewriteCond %{REQUEST_URI} !post.php
RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?s=$1

</ifModule>

Unfortunately, It seems to be directing all pages that aren't found to the blank post.php page instead of the error.php page. 不幸的是,它似乎将所有未找到的页面定向到空白的post.php页面,而不是error.php页面。

My site is here: http://tempertemper.net and here's a URL that doesn't exist http://tempertemper.net/this-url-does-not-exist 我的网站在这里: http : //tempertemper.net ,这是一个不存在的URL http://tempertemper.net/this-url-does-not-exist

It should take you to http://tempertemper.net/error 它应该带您到http://tempertemper.net/error

Thanks for taking a look :) 谢谢参观 :)

Because you are redirecting all paths which fit the [a-zA-Z0-9_-]+ pattern, this will simply send to your post.php script the query string s=this-url-does-not-exist . 因为您要重定向所有适合[a-zA-Z0-9_-]+模式的路径,所以这只会将查询字符串s=this-url-does-not-exist发送到您的post.php脚本中。 So it's up to your PHP script post.php to check whether this-is-not-a-url exists or not, as Apache cannot possibly know what blog post ID values are valid and which are not. 因此,由您的PHP脚本post.php来检查this-is-not-a-url存在,因为Apache可能无法知道哪些博客文章ID值有效,哪些无效。

As a side note, I'd recommend using /blog/2012-08-24-whatever as the path you give to visitors, rather than /2012-08-24-whatever . 作为附带说明,我建议使用/blog/2012-08-24-whatever作为您提供给访问者的路径,而不是/2012-08-24-whatever That way you can reserve paths for other functions, such as /images without having to write a new exception in your .htaccess file every time you need to create a new utility path. 这样,您就可以为其他功能(例如/images保留路径,而不必每次需要创建新的实用程序路径时都在.htaccess文件中写入新的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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