gpt4 book ai didi

php - 使用 htaccess 重写条件请求

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:40:18 24 4
gpt4 key购买 nike

我想完成以下(重写而不是重定向):

  1. searchPage.php?crs_category=variablesearchPage.php?crs_category=businesscategory/businesscategory/business/ 不区分大小写

也可以直接访问category/name,这是searchPage的重写

  1. index.php/namepage/name 不区分大小写。请注意,这是重写而不是重定向,我想使用干净的 url 访问 index.php/name

也可以直接访问page/name,这是index.php/name的重写

注意:- 它需要考虑空格,例如如果用户单击 searchPage.php?crs_category=html%20and%css,它会将 url 重写为 category/html and css

尝试过:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


RewriteCond %{THE_REQUEST} /searchPage.php\?crs_category=([^\s]+) [NC]
RewriteRule ^category/%1? [NE,NC,R,L]
RewriteRule ^category/([^/]+)/?$ /searchPage.php?crs_category=$1 [QSA,L,NC]


RewriteCond %{THE_REQUEST} /index\.php/(\S+) [NC]
RewriteRule ^ /page/%1 [NE,R,L]
RewriteRule ^page/([^/]+)/?$ index.php/$1 [NC,L]

问题:由于某种原因,该请求不适用于这些行:

RewriteCond %{THE_REQUEST} /searchPage.php\?crs_category=([^\s]+) [NC]
RewriteRule ^category/%1? [NE,NC,R,L]

另外,我不确定谁考虑到了空格。如果它的 searchPage.php?crs_category=html%20andcss 导致类别/html 和 css 忽略间距代码

更新:

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /

# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)[\s%20]+(.*)$ $1-$2 [E=NOSPACE:1]

# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]

RewriteCond %{THE_REQUEST} /searchPage\.php\?crs_category=([^&]+?)\s [NC]
RewriteRule ^ category/%1? [NE,R,L]

RewriteCond %{THE_REQUEST} /index\.php/(\S+) [NC]
RewriteRule ^ page/%1 [NE,R,L]

RewriteRule ^category/([^/]+)/?$ searchPage.php?crs_category=$1 [QSA,L,NC]

RewriteRule ^page/([^/]+)/?$ index.php/$1 [NC,L]

最佳答案

您应该在 index.php 之后匹配 / 而不是 ?:

您可以使用这些规则:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /searchPage\.php\?crs_category=([^&]+?)\s [NC]
RewriteRule ^ category/%1? [NE,R,L]

RewriteCond %{THE_REQUEST} /index\.php/(\S+) [NC]
RewriteRule ^ page/%1 [NE,R,L]

# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^(\S*)\s+(.*)$ $1-$2 [E=NOSPACE:1,DPI]

# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^(\S+)$ $1 [NE,R=302,L]

RewriteRule ^category/([^/]+)/?$ searchPage.php?crs_category=$1 [QSA,L,NC]

RewriteRule ^page/([^/]+)/?$ index.php/$1 [NC,L]

关于php - 使用 htaccess 重写条件请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31926161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com