简体   繁体   中英

How to create dynamic pages with .htaccess and mod_rewrite

I want to know what I need to put in the .htaccess to do the following.

When people call one of my pages, for example:

http://www.mydomain.com/cooks/New-York_NY.html
http://www.mydomain.com/cooks/Plantation_FL.html

that it would create these pages on the fly from a file called cookscities.php .

Then the page cookscities.php would pull the values from the URL for the city and state and place it on the page.

So for sample requested page: http www.mydomain.com/cooks/New-York_NY.html my page cookscities.php inserts value of URL on page content city value but remove the "-" if there is one and also places the state 2 letters.

So the page title would say:

Find Cooks in New York NY

In .htaccess add:

RewriteEngine on
RewriteRule /cooks/(.*)$ http://mydomain.com/cookscities.php?val=$1 [L]

This matches any URL beginning with /cooks/ and calls cookscities.php, passing in the part after "/cooks/" as the GET variable val .

In cookscities.php you could have:

<?php
list($city, $state) = explode('_', $_GET['val']);
$city = str_replace('-', ' ', $city);

echo "Find Cooks in $city $state";
?>

通常可以通过使用mod_rewrite完成此操作: http : //www.sitepoint.com/guide-url-rewriting/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM