首页 > 解决方案 > 将 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

解决方案


检查您的 .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]

推荐阅读