将URL重写到其他目录并更改扩展名

时间:2019-07-14 18:09:40

标签: .htaccess

我在一些广泛检查的重写中遇到了问题: 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罚款。

任何帮助表示赞赏。如果您需要更多信息,请询问。

1 个答案:

答案 0 :(得分:0)

在.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]