的.htaccess动态URL段添加到现有的重写规则 [英] .htaccess to add dynamic url segments to an existing rewrite rule

查看:207
本文介绍了的.htaccess动态URL段添加到现有的重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经制定这个规则

 #重写帐户查看
重写规则^([^ /] +)/([^ /] +)/ $ account.php类型1 = $&安培;?用户= $ 2 [NC,L]
 

这转化为 http://www.site.com/user/s2xi

但现在我想要得到更具创造性的URL,并追加一个主题的位置给它这样:

  http://www.site.com/user/s2xi/theme/slate/
 

这样我可以为实例访问的CSS文件的主题,我只需要键入

  http://www.site.com/user/s2xi/theme/slate/css/slate.css
 

我怎么能修改或添加其他重写规则,以便能够得到我想要的结果吗?

另外,我将如何能够运用和访问我的主题文件与新的URL在我的PHP code?

我目前的路径,我的主题是库/主题/用户/石板与子文件夹 / CSS / JS

我的尝试:

 #重写用户的主题定位
重写规则^([^ /] +)/([^ /] +)/主题/([^ /] +)/ $ account.php 1型= $&放大器;?用户= $ 2 $主题= $ 3 [NC,L ]
 

解决方案

我建议所有重定向请求一个文件,例如: 的index.php ,然后获得url使用 $ _ SERVER ['REQUEST_URI'] 。然后,您可以使用这些数据,例如爆炸了。然后你就可以动态地确定哪些网页将被加载。

您可以使用下面的code到所有请求重定向的index.php

  RewriteEngine叙述上

重写规则。*的index.php
 


你也可以排除特定文件类型被重定向到这个PHP文件。对于像CSS例如静态内容,JavaScript的等下code将,例如,排除所有文件,使用CSS,JS,PNG或JPG格式扩展名:

  RewriteEngine叙述上

重写规则\(CSS | JS | PNG | JPG)!$的index.php  


要排除确实存在的所有文件,可以使用下面的code:

  RewriteEngine叙述上

的RewriteCond%{} REQUEST_FILENAME!-f
重写规则。*的index.php  

So I have this rule in place

# Rewrite for account view
RewriteRule ^([^/]+)/([^/]+)/?$ account.php?type=$1&user=$2 [NC,L]

which translate to http://www.site.com/user/s2xi

but now I want to get even more creative with the url and append a theme location to it so:

http://www.site.com/user/s2xi/theme/slate/

so that I can for instance access the CSS file for the theme I would simply need to type in

http://www.site.com/user/s2xi/theme/slate/css/slate.css

how can I modify or add another RewriteRule to be able to get the results I want?

also, how would I be able to apply and access my theme files with the new url in my PHP code?

my current path to my themes is library/themes/users/slate with sub folders /css and /js

My attempt:

# Rewrite for user theme location
RewriteRule ^([^/]+)/([^/]+)/theme/([^/]+)/?$ account.php?type=$1&user=$2$theme=$3 [NC,L]

解决方案

I suggest redirecting all requests to one file, e.g. index.php and then get the url by using $_SERVER['REQUEST_URI']. You can then use this data and for example explode it. Then you will be able to dynamically determine which page will be loaded.

You can use the following code to redirect all requests to index.php:

RewriteEngine on

RewriteRule .* index.php


You could also exclude particular filetypes from being redirected to this PHP file. For example static content like CSS, JavaScript etc. The following code will, for example, exclude all files with a CSS, JS, PNG or JPG extension:

RewriteEngine on

RewriteRule !\.(css|js|png|jpg)$ index.php


To exclude all files that do exist, you could use the following code:

RewriteEngine on

RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule .* index.php

这篇关于的.htaccess动态URL段添加到现有的重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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