开发者

Handling page views with htaccess and php

Currently I'm appending the following url parameter:

www.somesite.com/?page_type=view

My php script uses this to determine which page view to load:

if (isset($_GET['page_type'])) {
    $page_type = $_GET['page_type'];
    $this->pageType($page_type);
} else {
    $this->home();
}

I would like to be able use the following url to achieve the same thing:

www.somesite.com/view

So I need to redirect all requests to index.php while maintaining the original url 开发者_高级运维input.

Then I can just use

$_SERVER['REQUEST_URI']

to get at the name of the page view.

Looking for some htaccess advice,

Thanks in advance!


You can use something along the lines of:

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

This will redirect all requests that don't resolve to actual files/directories to your index.php. The ?url=$1 will contain the request but this may be optional in your case as you can still just get it from $_SERVER['REQUEST_URI'].

I left a RewriteBase line in there (commented out) as uncommenting can help with come server setups.


Another option you have is to simply set up your htaccess file to redirect any 404 (unresolvable) requests into the index file with a flag set like this:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

From here you can use PHP's access to the $_SERVER['REQUEST_URI'] information to parse things out in PHP and manually push variables into the $_REQUEST superglobal as needed to "map" your search engine friendly links into the application.

This gives you a lot more scalability than simply forcing the /pagetypevar format on all urls.

What happens later when you want to nest pages into subcategories? /info/about_us, /info/contact_us What happens if you want to store other variables like items per page or pagination in the url? /products/1_My_supercool_item, /products/2_Another_item

Using a system that redirects all unresolved requests into the application framework, and allowing the application framework to do the remapping of urls will give you the most control and the most scalability.


Use a rewrite rule like this:

RewriteEngine On
RewriteRule /(home|view) index.php?page_type=$1


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /^(.*)$ /index.php?page_type=$1 [L]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜