如何使用虚拟目录、实际目录和变量组合使用 mod_rewrite 编写干净的 URL

问题描述 投票:0回答:1

我已经阅读了之前的问题并阅读了教程,但我不完全理解 mod_rewrite,所以我在这里发帖。 我当前的 .htaccess 是:

Options +FollowSymLinks
RewriteEngine On

#### 1st set of rules.... ####
#Using THE_REQUEST for catching references and using them later on.
RewriteCond %{THE_REQUEST} \s/(content)/index\.php\?var=(.*?)\s [NC]
RewriteRule ^ /%1/featured/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(content)/featured/(.*?)/?$ $1/index.php?var=$2 [QSA,NC,L]


#### 2nd set of rules.... ####
#Using THE_REQUEST for catching references and using them later on.
RewriteCond %{THE_REQUEST} \s/(profile)\.php\?userid=(.*?)\s [NC]
RewriteRule ^ /%1%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(profile)/(.*?)/?$ $1.php?userid=$2 [QSA,NC,L]

我有这个网址:www.域名 .com/content/cat/build 并指向 www.域名 .com/content/catcontent.php?catvar=build ??它会遵循什么 htaccess mod_rewrite 规则?

我已经添加了这个,但它破坏了网站......

   #### 3rd set of rules.... ####
    #Using THE_REQUEST for catching references and using them later on.
    RewriteCond %{THE_REQUEST} \s/(content)/(cat)/\catview\.php\?cattype=(.*?)\s [NC]
    RewriteRule ^ /%1%2? [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(content)/(cat)/catview.php/?$ $1.php?cattype=$2 [QSA,NC,L]

谢谢

我用谷歌搜索并阅读了旧帖子。 我已阅读教程。 我不太理解 mod_rewrite ,因为它看起来像一个字母数字单词沙拉,我正在寻求帮助

mod-rewrite
1个回答
0
投票

根据您展示的示例,请尝试遵循以下规则。在测试您的网址之前,请确保清除浏览器缓存

Options +FollowSymLinks
RewriteEngine On

#### 1st set of rules.... ####
#Using THE_REQUEST for catching references and using them later on.
RewriteCond %{THE_REQUEST} \s/(content)/index\.php\?var=(.*?)\s [NC]
RewriteRule ^ /%1/featured/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(content)/featured/(.*?)/?$ $1/index.php?var=$2 [QSA,NC,L]


#### 2nd set of rules.... ####
#Using THE_REQUEST for catching references and using them later on.
RewriteCond %{THE_REQUEST} \s/(profile)\.php\?userid=(.*?)\s [NC]
RewriteRule ^ /%1%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(profile)/(.*?)/?$ $1.php?userid=$2 [QSA,NC,L]

##3rd set of rules from here....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(content)/(cat)/(build)/?$ $1/$2$1.php?catvar=$3 [QSA,NC,L]
© www.soinside.com 2019 - 2024. All rights reserved.