简体   繁体   English

多个重写规则在.htaccess中不起作用

[英]Multiple Rewrite rules not working in .htaccess

This is my code in .htaccess : 这是我在.htaccess中的代码:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+product\.php\?course_name=([^\s&]+)&course_id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)([^/]+)/?$ /product.php?course_name=$1&course_id=$2 [L,QSA]

Which is working fine but now I have to check if a user enters a wrong address in my website like www.example.com/no-file-exists.php then it will redirect to my 404page.php which I build to show an error message. 这工作正常,但现在我必须检查用户是否在我的网站中输入了错误的地址,例如www.example.com/no-file-exists.php,然后它将重定向到我构建的404page.php来显示错误。信息。 So I have this rule RewriteRule ^ 404page.php and put it in my code then my previous rule will be overwritten so I don't know where to put in order to get all rules working, please help? 因此,我有此规则RewriteRule ^ 404page.php并将其放在我的代码中,然后我之前的规则将被覆盖,因此我不知道在哪里放置以便使所有规则正常工作,请帮忙吗?

On a side-note: The rule RewriteRule ^([^/]+)([^/]+)/?$ /product.php?course_name=$1&course_id=$2 [L,QSA] seems to be wrong. 旁注:规则RewriteRule ^([^/]+)([^/]+)/?$ /product.php?course_name=$1&course_id=$2 [L,QSA]似乎是错误的。 I think you forgot a / ;-) 我认为您忘记了/ ;-)

You can solve your problem by not writing another rewriterule, but instead make a handler for 404 errors. 您可以通过不编写其他重写器来解决问题,而可以针对404错误创建处理程序。 You can do this with ErrorDocument ( docs ). 您可以使用ErrorDocumentdocs )执行此操作。 You would end up with something like this: 您最终将得到如下结果:

ErrorDocument 404 /404page.php

Reminder: If the second argument ( /404page.php ) doesn't begin with a slash ( / ), then it is treated like a string of text to send with the 404 not found header. 提醒:如果第二个参数( /404page.php )并非以斜杠( / )开头,则将其视为带有404 not found标头发送的文本字符串。 You don't want that. 你不要那样

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

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