.htaccess重写规则不起作用(.htaccess rewrite rule does not work)

编程入门 行业动态 更新时间:2024-10-27 10:32:42
.htaccess重写规则不起作用(.htaccess rewrite rule does not work)

我在.htaccess中的重写规则遇到了一些麻烦,这里没有其他答案似乎可以解决问题!

基本上下面的代码可行

RewriteRule ^index.php$ test.php

但是,只要我添加任何其他内容,它就不会! 下面的两行代码根本不起作用

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

当我开始工作时,我最终想要从页面中获取项目标题并将其插入到URL中(如果您查看URL,就像这个站点一样)并将所有.php转换为.html但我似乎已经落在第一关!

任何人的想法? 任何帮助将不胜感激!

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!

最满意答案

请参阅以下对您的代码的编辑

#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]

如果这个规则

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

仅在id = 82时才匹配,那么它将无法工作,需要重写如下

#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]

编辑

我想采取project.php?id = 82并更改为project-82.html

请参阅以下规则

#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]

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]

更多推荐

本文发布于:2023-08-01 01:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   不起作用   规则   htaccess   rule

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!