htaccess的重定向,除了CSS和JavaScript所有的扩展有的、重定向、htaccess、JavaScript

2023-09-02 09:53:23 作者:一条咸鱼

我有一个的.htaccess 文件重定向任何扩展到非扩展的URL,并显示的 requestedfilename +的.php 的。它的伟大工程的(。*)的条件的一部分。

当我输入的 domain.com/file.html 或 domain.com/file.xml 的它显示的为file.php 的和URL看起来像的 domain.com/file 的。

我只是想知道如何exculde喜欢的.js和的CSS从ex pression扩展。我不希望他们重定向到其他任何PHP文件。

我试过的东西,如:(*。!(JS | CSS))(。*) 而不是 ,但我不能找到一个可行的解决方案..

目前的code是这样的:

 < IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteBase /

#
#显示任何要求延长name.php文件(。*)
#
重写规则^(。*)。(。*)$ 1  .PHP

#
#隐藏所有类型的扩展名(。*)的网址
#
的RewriteCond%{THE_REQUEST} ^ [AZ] +  S + (。*) SHTTP /.+
重写规则^(。+)。PHP $ 1 [R = 301,L]

#
#没有扩展名的URL PHP
#
的RewriteCond%{} REQUEST_FILENAME -f .PHP
的RewriteCond%{REQUEST_URI}!/ $
重写规则(。*)$ 1  .PHP [L]

< / IfModule>
 

解决方案 SEO工作者必备谷歌插件

如果你的Apache版本支持,您可以使用负向前查找,写的第一个重写规则是这样的:

 重写规则^(。*)(?!JS | CSS)。([^] *)$ $ 1  .PHP
 

[^] 部分确保了(。*)。相匹配的一切,直到最后,定位的负面先行在正确的位置。

I have a .htaccess file that redirect any extension to a non-extension url and displays the requestedfilename + .php. It works great with the (.*) part of the conditions.

When I type domain.com/file.html or domain.com/file.xml it displays the file.php and the url looks like domain.com/file.

I just would like to know how to exculde extensions like .js and .css from the expression. I don't want to redirect them to any other php file.

I tried things like: (.*!.(js|css)) instead of (.*) but I can't find a working solution...

The current code is this:

<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>

解决方案

If your version of Apache supports it, you may be able to use "negative lookahead" and write the first RewriteRule like this:

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

The [^.]part makes sure the (.*). matches everything until the last ., "positioning" the negative lookahead at the right spot.