gpt4 book ai didi

php - .htaccess 重定向除 css 和 javascript 之外的所有扩展名

转载 作者:可可西里 更新时间:2023-11-01 01:04:10 25 4
gpt4 key购买 nike

我有一个 .htaccess 文件,可以将任何扩展名重定向到非扩展名 url 并显示 requestedfilename + .php。它适用于条件的 (.*) 部分。

当我键入 domain.com/file.htmldomain.com/file.xml 时,它会显示 file.php 和网址看起来像 domain.com/file

我只想知道如何从表达式中排除 .js 和 .css 等扩展名。我不想将它们重定向到任何其他 php 文件。

我试过类似的东西:(.*!.(js|css)) 而不是 (.*) 但我找不到可行的解决方案.. .

目前的代码是这样的:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#
# to display the name.php file for any requested extension (.*)
#
RewriteRule ^(.*)\.(.*) $1\.php

#
# to hide all type of extensions (.*) of urls
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.(.*)\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]

#
# no extension url to php
#
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

</IfModule>

最佳答案

如果你的 Apache 版本支持它,你可以使用“negative lookahead”并像这样编写第一个 RewriteRule:

RewriteRule ^(.*)\.(?!js|css)([^.]*)$ $1\.php

[^.] 部分确保 (.*)\. 匹配所有内容,直到最后一个 .,“定位”在正确的位置进行负前瞻。

关于php - .htaccess 重定向除 css 和 javascript 之外的所有扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15202316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com