.htaccess多个参数的重写规则

编程入门 行业动态 更新时间:2024-10-20 13:42:23
本文介绍了.htaccess多个参数的重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

重写以下3个URL

example/index.php?page=search 到 example/search

example/index.php?page=profile&value=我的名字 到 http:// example。 com / myname

example/index.php?page=stats&type=daily 到 example/stats/daily

当前.htaccess的写法如下:

Current .htaccess is written as the following:

Options -MultiViews RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&value=([^\s&]+) [NC] RewriteRule ^ index/%1/%2? [R=302,L,NE] RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&type=([^\s&]+) [NC] RewriteRule ^ index/%1/%2? [R=302,L,NE] RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+) [NC] RewriteRule ^ index/%1/? [R=302,L,NE] RewriteRule ^index/([^/]+)/([^/]+)/?$ /index.php?page=$1&value=$2 [NC,L,QSA] RewriteRule ^index/([^/]+)/([^/]+)/?$ /index.php?page=$1&type=$2 [NC,L,QSA] RewriteRule ^index/([^/]+)/?$ /index.php?page=$1 [NC,L,QSA]

我需要从第一个和第三个URL中删除/ index /,从第二个URL中删除/ index / profile/。

I need to remove /index/ from the first and third URL, and /index/profile/ from the second one.

所有评论&欢迎提出建议。

All comments & suggestions are welcomed.

推荐答案

搜索和配置文件不能同时使用相同的正则表达式模式。例如,如果有人请求:

You can't have both the search and profile be the same regex pattern. For example, if someone requests:

example/index/foo

他们是要转到名为 foo的页面还是转到名为 foo的用户的个人资料?您需要添加将两者分开的内容,例如:

Are they trying to go to a page called "foo" or go to a profile of a user named "foo"? You need to add something that separates the two, maybe something like:

Options -MultiViews RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} /index\.php\?page=profile&value=([^\s&]+) [NC] RewriteRule ^ profile/%1? [R=302,L,NE] RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&type=([^\s&]+) [NC] RewriteRule ^ page/%1/%2? [R=302,L,NE] RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)(\ |$) [NC] RewriteRule ^ page/%1? [R=302,L,NE] RewriteRule ^profile/([^/]+)/?$ /index.php?page=profile&value=$1 [NC,L,QSA] RewriteRule ^page/([^/]+)/([^/]+)/?$ /index.php?page=$1&type=$2 [NC,L,QSA] RewriteRule ^page/([^/]+)/?$ /index.php?page=$1 [NC,L,QSA]

这会使您的网址看起来像:

This makes your urls look like:

example/profile/myname example/page/search example/page/stats/daily

更多推荐

.htaccess多个参数的重写规则

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

发布评论

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

>www.elefans.com

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