.htaccess - 将 URL 重写到不同的目录并更改扩展名
问题描述
我有一些重写的问题,我已经广泛检查过: https ://htaccess.madewithlove.be/
但是在 Web 服务器上不能按预期工作。
我的规则是:
ReWriteEngine On
RewriteRule ^$ /content/index.php [L]
RewriteRule ^(.*)/(.*).html$ /content/$1/$2.php [L]
RewriteRule ^(.*)/$ /content/$1/index.php [L]
RewriteRule ^(.*)$ /content/$1/index.php [L]
示例 URL 是:
https://domain.tld -> /content/index.php
https://domain.tld/ -> /content/index.php
https://domain.tld/path -> /content/path/index.php
https://domain.tld/path/ -> /content/path/index.php
https://domain.tld/path/page.html -> /content/path/page.php
https://domain.tld/path/more -> /content/path/more/index.php
https://domain.tld/path/more/ -> /content/path/more/index.php
https://domain.tld/path/more/page.html -> /content/path/more/page.php
我收到内部服务器错误,即使页面上没有任何内容。如果我删除路由,我会得到 404 罚款。
任何帮助表示赞赏。如果您需要更多信息,请询问。
解决方案
检查您的 .htaccess 文件顶部的此规则
RewriteEngine on
RewriteBase /content/
# path/more/page.html -> /content/path/more/page.php
RewriteRule ^(.*)\/(.*)\/(.*)\.html$ /$1/$2/$3.php [L]
# path/more/ -> /content/path/more/index.php
RewriteRule ^(.*)\/(.*)\/$ /$1/$2/index.php [L]
# path/page.html -> /content/path/page.php
RewriteRule ^(.*)\/(.*)\.html$ /$1/$2.php [L]
# path/more -> /content/path/more/index.php
RewriteRule ^(.*)\/(.*)$ /$1/$2/index.php [L]
# path/ -> /content/path/index.php
RewriteRule ^(.*)\/$ /$1/index.php [L]
# path -> /content/path/index.php
RewriteRule ^(.*)$ /$1/index.php [L]
# / -> /content/index.php
RewriteRule ^\/$ /index.php [L]
# https://domain.tld -> /content/index.php
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.tld [NC]
RewriteRule ^ /index.php [L]
推荐阅读
- contentful - 内容丰富的媒体文件名/扩展名验证
- java - 用于从 sererate JVM 实例调用的重复单元和批处理测试的 Java 配置缓存?
- python - 如何使我的 Python 替换不贪心?
- visual-studio-2010 - Azure DevOps - Visual Studio 2010
- asp.net-mvc - EF Code First:在 Code First 生成后未在 NOT NULL int 列中添加“必需”
- regex - Find lines with numbers
- ironpython - 如何根据 Spotfire 中列的数据类型使用 IronPython 脚本设置过滤器类型/属性
- html - 我正在 div 元素上实现选择/取消选择。我可以一次单击选择 div,但无法在下次单击同一个 div 时取消选择它?
- amazon-s3 - AWS S3 上传在 nativescript 上失败
- angular - 如何使用href Angular 6将变量绑定到url参数?