首页 > 解决方案 > 如何为多个子目录制定htaccess重写规则

问题描述

我有 3 个子目录,其中包含index.php具有相同 url 参数的文件:

我正在使用以下代码.php从 URL中删除

RewriteEngine On

options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

如何使它对 SEO 友好https://example.com/one/index/onestar。以及如何使其动态以在将来添加多个目录,例如https://example.com/four/index/onestar

标签: regexapache.htaccessmod-rewritecpanel

解决方案


您可以在站点根目录 .htaccess 中使用此代码:

Options -MultiViews
RewriteEngine On

# http => https
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# /one/index/onestar => /one/index.php?c=onestar
RewriteRule ^([\w-]+)/index/([\w-]+)/?$ $1/index.php?c=$2 [L,NC,QSA]

# hide .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

推荐阅读