質問

Well it was kind of difficult what wordings for the title, yet I'm quite interested how this thing works.

Assuming that I have this .htaccess on my root folder.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

For example

I have this url that consists of whatever content it has.

www.myurl.com/foo

Then if I go to other page it would be like

www.myurl.com/foo/bar

So meaning, I'm using the foo as their base once I redirect to other pages.

So my structure of root folder is..

index.php
foo.php
bar.php

So if www.myurl.com/foo I'm opening the foo.php, but then if I access www.myurl.com/foo/bar I'm opening bar.php with a base of file name of foo.php. Logically thinking it's wrong.

But are there any way of doing this?

Real live desired result is..

www.myurl.com/campaigns

if I go to child page of campaigns it would be

www.myurl.com/campaigns/create

Any help would be appreciated.

役に立ちましたか?

解決

In order to accomplish this, you need a router; Apache rewrite tools are not suited to handling this alone. Typically you use a front controller (often called index.php) that everything is rewritten to, passing the whole url as a parameter. The front controller then starts the router, which fires up the correct controller to handle the request.

Your .htaccess then looks something like:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

Take a look at the Symfony router component, and read the first chapter of the Symfony book for the general idea of how that all works.

他のヒント

Anyways, Thanks for the answer @Bryan Agee.

I've found a great site that talks about URL Rewriting, especially for the people who doesn't even know how it works including me.

To anyone interested, here's the link of it.

http://coding.smashingmagazine.com/2011/11/02/introduction-to-url-rewriting/

It's very easy to understand.

For the solution on my problem, here it is.

RewriteEngine On

RewriteRule   ^campaigns/?$             src/campaigns/index.php [NC]
RewriteRule   ^campaigns/create/?$          src/campaigns/create.php [NC]

And if I want to put parameters on the link

#RewriteRule   ^campaigns/create/(.+)$   src/campaigns/create.php?cid=$1 [L]

For furthermore understanding, visit the link above. :D

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top