如何在htaccess中为PHP中的特定文件夹重写URL

时间:2014-11-14 06:00:52

标签: php apache .htaccess url-rewriting

我需要帮助重写URLS。这是给定的

Site URL: http://example.com:8000/myapp

目前的htaccess

Allow from localhost

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA] 

我的网站位于

/var/www/htdocs/myapp

虚拟主机配置

<VirtualHost *:8000>
    DocumentRoot "/var/www/htdocs"
    ServerName example.com
    ErrorLog "/var/log/apache2/custom/a_local_error_log"
    CustomLog "/var/log/apache2/custom/a_local_access_log" common
    <Directory "/var/www/htdocs">
            Options Indexes FollowSymLinks
            Require all granted
            Order allow,deny
            Allow from all
            AllowOverride All
    </Directory>
</VirtualHost>

问题是我的资产与应用程序位于同一文件夹中,但包含资产的代码是绝对的。例如:

/_js/jquery.js
/_js/myjs.js

是否可以将其重写为:

/_js/myqpp/jquery.js
/_js/myapp/myjs.js

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以在RewriteEngine on下面插入此规则:

RewriteRule ^(_js)/([^/.]+\.js)$ /$1/myapp/$2 [L,NC,R=302]

答案 1 :(得分:0)

我相信以下内容应该适用于此:

RewriteRule ^/_js/jquery.js$ /_js/myapp/jquery.js [NC,L]
RewriteRule ^/_js/myjs.js$ /_js/myapp/myjs.js [NC,L]

在你的其他重写规则之前尝试一下。