这是我目前的htaccess
<IfModule mod_rewrite.c>
# turn on rewrite engine
RewriteEngine on
# if request is a directory, make sure it ends with a slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*/[^/]+)$ $1/
# if not rewritten before, AND requested file is wikka.php
# turn request into a query for a default (unspecified) page
RewriteCond %{QUERY_STRING} !wakka=
RewriteCond %{REQUEST_FILENAME} wikka.php
RewriteRule ^(.*)$ wikka.php?wakka= [QSA,L]
# if not rewritten before, AND requested file is a page name
# turn request into a query for that page name for wikka.php
RewriteRule ^(.*)$ wikka.php?wakka=$1 [QSA,L]
</IfModule>
我目前的网址结构是
www.domain.com/site/pool/Page_Example_Test
www.domain.com/site/pool/Page_Example_Test/edit
www.domain.com/site/pool/Page_Example_Test/edit?id=1
www.domain.com/site/pool/Page_Example_Test/history
www.domain.com/site/pool/Page_Number_Room
www.domain.com/site/pool/Page_Number_Room/edit
如何以这种方式访问它们
www.domain.com/site/pool/Page/Example/Test
www.domain.com/site/pool/Page/Example/Test/edit
www.domain.com/site/pool/Page/Example/Test/edit?id=1
www.domain.com/site/pool/Page/Example/Test/history
www.domain.com/site/pool/Page/Number/Room
www.domain.com/site/pool/Page/Number/Room/edit
让htaccess只改变那些&#34; /&#34;为&#34; _&#34; 只有/ history和/ edit完成了url名称,正常情况下没有/ edit或/ history。
答案 0 :(得分:1)
如果页面名称中有一到五个,例如
Page
Page/Page
Page/Page/Page
Page/Page/Page/Page
Page/Page/Page/Page/Page
并且每个元素都以大写字母开头,并且网址的每个其他部分都只是小写,那么您可以通过四个替换来完成此操作(仅一个部分不需要替换)。例如,有三件:
([A-Z][a-z]+)\/([A-Z][a-z]+)\/([A-Z][a-z]+)(\/)?
$1_$2_$3$4
你可以尝试here(虽然我不明白为什么它只替换每行中的空格而是最后一行)。
注意:
(...)
都是captured \/?
为optional,是潜在的“编辑”或“历史记录”之前的斜杠。/
,但这样做更安全:\/
。$[number]
都是capture group reference 此答案中的所有链接均来自Stack Overflow Regular Expressions FAQ。请考虑将其加入书签以供将来参考。特别是,请参阅底部的在线正则表达式测试人员列表,以便您自己尝试。
答案 1 :(得分:0)
将此代码放在/site/pool/.htaccess
:
RewriteEngine On
RewriteBase /site/pool/
RewriteRule "^(Page)/([^/]+)/([^/]+/.*)$" /$1/$2_$3 [L]
RewriteRule "^(Page)/([^/]+)/([^/]+)$" /$1/$2_$3 [L]