Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
760 views
in Technique[技术] by (71.8m points)

php - .htaccess to add dynamic url segments to an existing rewrite rule

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...