URL Rewrite .htaccess

2019-06-24 13:24发布

问题:

I'm getting ready to develop a web site with the following url structure.

I'm a newbie to url rewrites and would like to know the best way to handle this.

http://domain.com                    index.php
http://domain.com/about              about.php
http://domain.com/agencies           agencies.php
http://domain.com/contact            contact.php
http://domain.com/publisher          publisher.php
http://domain.com/publisher/sign_up  publisher_signup.php
http://domain.com/agencies/sign_up   agencies_signup.php
http://domain.com/agencies/login     login.php
http://domain.com/advertiser         advertiser.php
http://domain.com/advertiser/sign_up advertiser_signup.php
http://domain.com/advertiser/login   login.php

What would be the most efficient htaccess rewriterule?

Should I just manually enter each line with a rewrite or is there some good search/replace I could use?

I'm thinking long term the number of slashes in the url could be the most 4 for example

 http://domain.com/area/sub_area/sub_area2/sub_area3

Any help would be greatly appreciated.

回答1:

I'm sorry but your "URL" transformation are not homogeneous (= it's not possible to "generalize" because they do not share exactly the same principles) so here's the best I could do:

RewriteEngine On
^/?$ index.php [NC,QSA,L]
^/?about$ about.php [NC,QSA,L]
^/?(agencies|contact|publisher|advertiser)$ /$1.php [NC,QSA,L]
^/?(publisher|agencies|advertiser)/sign_up$  /$1_signup.php [NC,QSA,L]
^/?(publisher|agencies|advertiser)/login$    /login.php [NC,QSA,L]

But if you want "homogeneous" stuff that do not perfectly fit with what you're asking, you may have "cleaner" rules like this:

RewriteEngine On
^/?$ index.php [NC,QSA,L]
^/?about$ about.php [NC,QSA,L]
^/?(agencies|contact|publisher|advertiser)$ /$1.php [NC,QSA,L]
^/?(agencies|contact|publisher|advertiser)/sign_up$ /$1_signup.php [NC,QSA,L]
^/?(agencies|contact|publisher|advertiser)/login$ /login.php [NC,QSA,L]

And if you really want to centralize everything, you could do (= pass the type to the php file):

RewriteEngine On
^/?$ index.php [NC,QSA,L]
^/?about$ about.php [NC,QSA,L]
^/?(agencies|contact|publisher|advertiser)$ /$1.php [NC,QSA,L]
^/?(agencies|contact|publisher|advertiser)/(sign_up|login)$ /$2.php?type=$1 [NC,QSA,L]