doujian1954 2013-03-04 05:07
浏览 68
已采纳

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

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>

展开全部

  • 写回答

2条回答 默认 最新

  • duanmei4149 2013-03-04 05:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部