.htaccess url重写GET参数并删除.php而不破坏javascript / css

时间:2014-12-30 10:45:07

标签: .htaccess mod-rewrite

很抱歉,如果我严重解释我的问题,有点难以解释出现了什么问题*
我一直在玩.htaccess文件一段时间了,试着让它发挥作用 我的网站目录中有以下文件:

- index.php
- generator.php
- menu.php
- gift-history.php
- stats.php

- images/
    - bg-stars.png
    - avatar.png

- css/
    - style.css
    - glow.css
    - background.css

- js/
    - script.js
    - jquery.js
    - destiny.js
    - snowflake.js

这三个文件夹是images,css and js generator.php需要一个$_GET参数来知道要加载的发电机 到目前为止,我一直在使用以下网址:
http://tryhardhusky.com/generator.php?t=destiny
对于其他页面,我在.htaccess文件中包含以下代码:

php_value display_errors Off
php_flag magic_quotes 1
php_flag magic_quotes_gpc 1
php_value mbstring.http_input auto
php_value date.timezone America/New_York
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

这几乎删除了文件名中的.php扩展名,因此文件将是:
http://tryhardhusky.com/gift-history.php
变为:
http://tryhardhusky.com/gift-history

我正在尝试获取以下页面:
http://tryhardhusky.com/generator.php?t=destiny
重写为 http://tryhardhusky.com/generator/destiny

我以前的所有尝试都破坏了js和css,加载页面时返回404错误(尽管位置正确)
[找不到错误404:http://tryhardhusky.com/js/jquery.js]

任何线索如何在不破坏它的情况下做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以在htaccess

中替换当前代码
Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# rewrite /generator.php?t=XXX to /generator/XXX
RewriteCond %{THE_REQUEST} \s/generator\.php\?t=([^\s&]+)\s [NC]
RewriteRule ^ generator/%1? [R=301,L]

# hide PHP extension for existing files
RewriteCond %{THE_REQUEST} \s/(.+?)\.php(?:\s|\?) [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %1? [R=301,L]

# internally rewrite /generator/XXX to /generator.php?t=XXX
RewriteRule ^generator/([^/]+)$ generator.php?t=$1 [L]

# internally rewrite to PHP file extension (if existing)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]