문제

Hello I was trying to come up with the solution to my problem, but I just was not able to. So here is my problem:

What I used was a .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule  ^.+(.*)/(.*)$   ./index.php?mesto=$1&den=$2 [QSA,NC,L]

It will display the url well. as www.site.com/CITY/DAY for example ../Prague/30.3.2014 but i need it to be more complex.

What I need is to have additional parameters, such as Bar or Restaurant in the url for example www-site.com/Prague/30.3.2014/p/bar/restaurant and other time I might have www-site.com/Prague/30.3.2014/p/pizza/bar

That part I have no idea how to do, because I have 5 different parameters

I imagine that the raw url would look this index.php?city=Prague&day=30.3.2014&p1=0&p2=0&p3=0&p4=0&p5=0 where p1 to p5 are the parameters being active (0 not 1 yes).

I don't understand how to detect what parameters are active and how to properly display the pretty url. Could you please help me?

도움이 되었습니까?

해결책

Use

RewriteRule ^(.*)$ ./index.php [QSA,NC,L]

This will redirect all your requests to a single index.php that parses the uri with something like this:

<?php 
    // Example URI: /florence/30-06-2009

    // Remove first slash from REQUEST_URI
    $uri = substr($_SERVER['REQUEST_URI'],1);

    // Get an array with portions between slashes.
    $splittedURI = explode("/", $uri);

    // Here you get your city (or anything else that you want)
    $city = array_unshift($splittedURI); // In example, $city = "florence"

    // Remaining itens in $splittedURI are the arguments/parameters to your page
    // Like this:
    $date = $splittedURI[0]; // In example, $date = "30-06-2009"
?>

Remember that this is just and example, and you should do additional verifications to avoid PHP exceptions.

다른 팁

If you need complicated routing (and if you sure you want to create your own router instead of using a ready solution as ZF, Symfony etc.) you're better off just passing the whole request uri to a php router object. There you can as complex router logic as you need.

So basically, loose the parsing in the rewrite rule:

RewriteRule  ^(.*)$   ./index.php?route=$1 [QSA,NC,L]

Then you can let the index.php create a router object that can parse the route parameter and delegate the job where it needs to.

I'd recommend reading up about existing routing solutions though.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top