htaccess - 除了少数页面之外,如何将所有子页面重写到一个目录 - css、js、图像文件的问题也被重写
htaccess - how to rewrite all sub pages to one directory except few pages - issue with css, js, image files also rewritten
我正在创建一个展示产品的网站
https://example.com/pencil should rewrite like https://example.com/item/index.php?id=pencil
但是,我还有一些其他页面,如关于,联系等,不应该重写到子目录
https://example.com/about -> https://example.com/about.php
这里,我在本地测试,所以实际的 url 会像 http://localhost/example.com/about 或 http://localhost/example.com/pencil
所以,我将 RewriteBase 添加为 /example.com/
RewriteEngine On
ErrorDocument 404 /example.com/404.php
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /example.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home/ index.php [L,END]
RewriteRule ^home index.php [L,END]
RewriteRule ^about-us/ about.php [L,END]
RewriteRule ^about-us about.php [L,END]
RewriteRule ^contact-us/ contact.php [L,END]
RewriteRule ^contact-us contact.php [L,END]
RewriteRule ^(.*)/$ item/index.php?url= [QSA,L]
RewriteRule ^(.*)$ item/index.php?url= [QSA,L]
</IfModule>
使用上面的 htaccess 代码,我可以将这两个页面重写为各自的 php 文件。但是,css 和 js 文件没有加载。
我是 htaccess 的初学者。任何帮助将不胜感激。
问题是js和css文件会被重写。
RewriteCond 仅适用于下一个 RewriteRule,因此您需要将其添加到所有内容中。所以你应该在每个 RewriteRule 之前添加它们。
# Add RewriteCond to avoid matching .css and .js files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ item/index.php?url= [QSA,L]
# Add RewriteCond to avoid matching .css and .js files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ item/index.php?url= [QSA,L]
</IfModule>
或者在匹配之前添加如下内容,这样css和js文件会更早匹配
RewriteEngine On
ErrorDocument 404 /example.com/404.php
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /example.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home/ index.php [L,END]
RewriteRule ^home index.php [L,END]
RewriteRule ^about-us/ about.php [L,END]
RewriteRule ^about-us about.php [L,END]
RewriteRule ^contact-us/ contact.php [L,END]
RewriteRule ^contact-us contact.php [L,END]
# match .css and .js file and add the END flag so no more rules will be applied.
RewriteRule "^(.*)\.css$" - [END]
RewriteRule "^(.*)\.js$" - [END]
RewriteRule ^(.*)/$ item/index.php?url= [QSA,L]
RewriteRule ^(.*)$ item/index.php?url= [QSA,L]
</IfModule>
另一种解决方案是匹配所有现有文件并禁用对这些文件的任何进一步匹配。
# Only match file that exists
RewriteCond "%{REQUEST_FILENAME}" -f
# Match any files (.*) that matches the RewriteCond. Don't apply changes (-) and apply the END flag.
RewriteRule .* - [END]
我正在创建一个展示产品的网站
https://example.com/pencil should rewrite like https://example.com/item/index.php?id=pencil
但是,我还有一些其他页面,如关于,联系等,不应该重写到子目录
https://example.com/about -> https://example.com/about.php
这里,我在本地测试,所以实际的 url 会像 http://localhost/example.com/about 或 http://localhost/example.com/pencil 所以,我将 RewriteBase 添加为 /example.com/
RewriteEngine On
ErrorDocument 404 /example.com/404.php
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /example.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home/ index.php [L,END]
RewriteRule ^home index.php [L,END]
RewriteRule ^about-us/ about.php [L,END]
RewriteRule ^about-us about.php [L,END]
RewriteRule ^contact-us/ contact.php [L,END]
RewriteRule ^contact-us contact.php [L,END]
RewriteRule ^(.*)/$ item/index.php?url= [QSA,L]
RewriteRule ^(.*)$ item/index.php?url= [QSA,L]
</IfModule>
使用上面的 htaccess 代码,我可以将这两个页面重写为各自的 php 文件。但是,css 和 js 文件没有加载。
我是 htaccess 的初学者。任何帮助将不胜感激。
问题是js和css文件会被重写。 RewriteCond 仅适用于下一个 RewriteRule,因此您需要将其添加到所有内容中。所以你应该在每个 RewriteRule 之前添加它们。
# Add RewriteCond to avoid matching .css and .js files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ item/index.php?url= [QSA,L]
# Add RewriteCond to avoid matching .css and .js files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ item/index.php?url= [QSA,L]
</IfModule>
或者在匹配之前添加如下内容,这样css和js文件会更早匹配
RewriteEngine On
ErrorDocument 404 /example.com/404.php
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /example.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home/ index.php [L,END]
RewriteRule ^home index.php [L,END]
RewriteRule ^about-us/ about.php [L,END]
RewriteRule ^about-us about.php [L,END]
RewriteRule ^contact-us/ contact.php [L,END]
RewriteRule ^contact-us contact.php [L,END]
# match .css and .js file and add the END flag so no more rules will be applied.
RewriteRule "^(.*)\.css$" - [END]
RewriteRule "^(.*)\.js$" - [END]
RewriteRule ^(.*)/$ item/index.php?url= [QSA,L]
RewriteRule ^(.*)$ item/index.php?url= [QSA,L]
</IfModule>
另一种解决方案是匹配所有现有文件并禁用对这些文件的任何进一步匹配。
# Only match file that exists
RewriteCond "%{REQUEST_FILENAME}" -f
# Match any files (.*) that matches the RewriteCond. Don't apply changes (-) and apply the END flag.
RewriteRule .* - [END]