regex - 如何为多个子目录制定htaccess重写规则
问题描述
我有 3 个子目录,其中包含index.php
具有相同 url 参数的文件:
example https://example.com/one/index.php?c=onestar
https://example.com/two/index.php?c=onestar
https://example.com/three/index.php?c=onestar
我正在使用以下代码.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
解决方案
您可以在站点根目录 .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]
推荐阅读
- css - CSS右浮动不能改变大小和大小不匹配css
- python - 如何在 python 3 上生成正弦波音调
- excel - 适用于 Microsoft Office (AMO) 的 Excel 加载项 - 使用 VBA 删除存储过程对象和保留值
- java - 使用 Streams 展平 Java 对象
- linux - 在 Yocto 中使用串行控制台登录时启用 root 密码验证
- java - 基本的 MVC 结构问题
- javascript - 响应式导航栏 - 单击时移动的下拉符号
- html - 如何阻止音频元素重叠导航
- visual-studio-code - 防止 VS Code 在另存为时将光标移动到文件开头
- reporting-services - 使用 IIF() 函数的 SSRS 报表生成器表达式无法正确评估包含“<”和“>”字符的条件