清洁的.htaccess URL和相对路径 [英] .htaccess clean URL and relative paths

查看:278
本文介绍了清洁的.htaccess URL和相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的.htaccess以下是:

 的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^([^ /] +)$ /index.php?type=cat&id=$1 [QSA,L]
重写规则^页/([^ /] +)$ /index.php?type=page&name=$1
 

这似乎工作得很好,但相对路径的图片和css文件里面的index.php成为在第二种情况下(页)坏了。不工作。在第二种情况下,所有图像都指向网页/图片/而不是图像/

除了硬编码的实际路径的图像,有没有解决这个问题的任何其他方式?

图片,CSS,JS文件夹位于根。这是怎样的根看起来像

 的.htaccess
的index.php
图片/
CSS /
JS /
 

解决方案

的RewriteCond指令只适用于规则直接下them.Try的follwing

 的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^([^ /] +)$ /index.php?type=cat&id=$1 [QSA,L]

的RewriteCond%{REQUEST_URI} \(CSS | JS |?JPE G | GIF | PNG)!$ [NC]
重写规则^页/([^ /] +)$ /index.php?type=page&name=$1 [L]

以图像#rewrite请求页/图片
的RewriteCond%{REQUEST_URI} ^ /页(/图像/.+)$ [NC]
重写规则。 %1 [L]
 

编辑。 修改重写网页/图片到图片

I have the following in my .htaccess which is :

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?type=cat&id=$1 [QSA,L]
RewriteRule ^page/([^/]+)$ /index.php?type=page&name=$1

This seems to work just fine but relative paths to pictures and css files inside of index.php become broken in the second case (Page). did not work. In second case, all images are pointing to page/images/ instead of image/

Other than hardcoding the actual path to images, is there any other way to fix this?

images, css, js folders are located in the root. This is how the root looks like

.htaccess
index.php
images/
css/
js/

解决方案

RewriteCond directives only apply to the rule directly following them.Try the follwing

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?type=cat&id=$1 [QSA,L]

RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^page/([^/]+)$ /index.php?type=page&name=$1 [L]

#rewrite requests for page/images to images
RewriteCond %{REQUEST_URI} ^/page(/images/.+)$ [NC]
RewriteRule . %1 [L]

EDIT. Modified to rewrite page/images to images

这篇关于清洁的.htaccess URL和相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆