regex - .HTAccess- 如何修改文件名中得分低的URL作为子目录?

regex - .HTAccess- 如何修改文件名中得分低的URL作为子目录?,第1张

假设我有一个包含以下文件的目录:

news_sports.html
news_food.html
news.html
index.html

如何使用.Htaccess进行操作,以便http://example.com/en-gb/news/sports访问我的网站时,可以使用news_sports.html和http://example.com/en-gb/news/food读取news_food.html和{{3} (在结尾处有或没有斜线)读出news.html

我当前的.Htaccess文件:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteCond %{DOCUMENT_ROOT}/_\.html -f
RewriteRule ^en-gb/([^/] )/([^/.] )/?$ _.html [L,NC]

RewriteCond %{DOCUMENT_ROOT}/\.html -f
RewriteRule ^(. ?)/?$ .html [L]

RewriteCond %{DOCUMENT_ROOT}/\.php -f
RewriteRule ^(. ?)/?$ .php [L]

RewriteRule ^en-gb(.*)$ /site/views/en/gb/ [L,NC]

目录结构

HTDOCS
  ->site
    ->views
       ->en
         ->gb
            ->index.html
            ->news.html
            ->news_sport.html
            ->news_food.html
         ->us
         ->au

总结一下,我想要它,以便当用户使用以下网址进入我的网站时:http://example.com/en-gb/news apache需要从HTDOCS提供 - > site-> views-> en-> gb - > news_sports.html

提前致谢

最佳答案:

1 个答案:

答案 0 :(得分:1)

您可以将新规则用于下划线条件:

RewriteEngine on

RewriteRule ^en-gb/([^/] )/([^/.] )/?$ /site/views/en/gb/_.html [L,NC]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteCond %{DOCUMENT_ROOT}/\.html -f
RewriteRule ^(. ?)/?$ .html [L]

RewriteCond %{DOCUMENT_ROOT}/\.php -f
RewriteRule ^(. ?)/?$ .php [L]

RewriteRule ^en-gb(.*)$ /site/views/en/gb/ [L,NC]
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复