开发者

.htaccess: mod_rewrite

开发者 https://www.devze.com 2023-03-29 17:14 出处:网络
Here is what I\'m trying to accomplish: (this .htaccess file is located in mysite.com/test/): http://mysite.com/test/admin go to http://mysite.c开发者_如何学Com/test/admin/index.php

Here is what I'm trying to accomplish: (this .htaccess file is located in mysite.com/test/):

http://mysite.com/test/admin go to http://mysite.c开发者_如何学Com/test/admin/index.php

http://mysite.com/test/contact go to http://mysite.com/test/contact.php

http://mysite.com/test/salt-lake-city/ go to http://mysite.com/test/index.php/city=salt-lake-city

http://mysite.com/test/salt-lake-city/deals/ go to http://mysite.com/test/deals.php?city=salt-lake-city

To start, I have:

RewriteRule ^(.*)/(.*)\.php$ $2.php?city=$1 [L]

(this handles the last 2). But, when I try to add the admin clause:

RewriteRule ^admin/ admin/index.php [L]

RewriteRule  ^(.*)/(.*)\.php$ $2.php?city=$1 [L]

It messes up (the css is out of whack) etc.

Any thoughts?


Well .. if I had to do these rewrite rules, I would do them like this:

Options +FollowSymLinks -MultiViews
RewriteEngine On

# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

# admin rewrite
RewriteRule ^admin$ admin/index.php [L]

# rewrite /contact --> /contact.php (and similar)
# add .php extension if such file does exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
# OR
# alternatively specify their names directly
# plus it is more precise for the example you have provided
# (that's if you need to rewrite only specific pages)
RewriteRule ^(contact|about|something)$ $1.php [L]

# /salt-lake-city/ --> /index.php?city=salt-lake-city
RewriteRule ^([^/]+)/$ index.php?city=$1 [QSA,L]

# /salt-lake-city/deals/ --> /deals.php?city=salt-lake-city
RewriteRule ^([^/]+)/deals/$ deals.php?city=$1 [QSA,L]

To start, I have:

RewriteRule ^(.*)/(.*)\.php$ $2.php?city=$1 [L]

(this handles the last 2).

Sorry, but I do not see how "it will handle the last 2". I see no .php in the last two URL examples you have provided.

It messes up (the css is out of whack) etc.

Well -- let's see how it will work with my rules. In any case -- it may also depends how you wrote links to css/images/js.

0

精彩评论

暂无评论...
验证码 换一张
取 消