开发者

Easy mod_rewrite - So I'll never have to think about it again

开发者 https://www.devze.com 2022-12-14 10:13 出处:网络
Not sure how you\'ll take this question but... Whenever I try to make my URLs look pretty I always end up messing around for too long and it\'s simply not worth the trouble. But the end effect is goo

Not sure how you'll take this question but...

Whenever I try to make my URLs look pretty I always end up messing around for too long and it's simply not worth the trouble. But the end effect is good if it were a simple task.

So what I want to do is create a method which in the end would achive something开发者_如何学Python like...

index.php?do=user&username=MyUsername //This becomes...
/user/MyUsername //...that
index.php?do=page&pagename=customPage //And this becomes...
/page/customPage //...that
index.php?do=lots&where=happens&this=here //This also becomes...
/lots/happens/here //...that
index.php?do=this&and=that&that=this&and=some&more=too //And yes...
/this/that/this/some/more //This becomes this

So then I just make a nice .htacess file that I'll never have to look at again. Everything will be better in the world because we have pretty URLs and my head didn't hurt in the making.


You can use a different approach of throwing the url in a single parameter, and parse it in your application.

So the apache rewrite rule would look like:

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

which will convert your urls as follows:

/user/MyUsername => index.php?q=/user/MyUsername
/page/customPage => index.php?q=/page/customPage
...

In your app, you then have a $_GET['q'] variable, which you can split by '/', and have your arguments in order. In PHP it would be something like:

$args = explode('/', $_GET['q']);

$args will be an array with 'user', 'MyUserName', etc.

This way you will not have to touch your .htaccess again, just your app logic.


For /user/MyUsername ==> index.php?do=user&username=MyUsername and /page/customPage ==> index.php?do=page&pagename=customPage, you can use:

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?do=$1&$1name=$2 [L]

But I don't think you can write a catch-all rule for /lots/happens/here and /this/that/this/some/more because you need to tell mod_rewrite how to translate the two urls.

Remember, mod_rewrite has to translate /lots/happens/here into index.php?do=lots&where=happens&this=here and not the other way around.


The best approach would be to delegate your application to generate the “pretty URLs” as well as parse and interpret them and to use mod_rewrite only to rewrite the requests to your application with a rule like this one:

RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

This rule will rewrite all requests that can not be mapped directly to an existing file to the index.php. The originally requested URL (more exact: the URL path plus query) is then available at $_SERVER['REQUEST_URI'].

0

精彩评论

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