简体   繁体   中英

SEO Friendly URL to Dynamic URL using PHP

Currently I have a url like this

http://<client_name>.website.com/index.php?function_name&cat=32

I want to set things up so that our Marketing people can publish url's like

http://<client_name>.website.com/<parent_category>/<category>   

The "cat=XX" will be generated of the last <category> only. But marketing wants to use the parent category in their campaigns. Currently we pass all of URL's through index.php in the html root directory (this will become important later).

I've tried several solutions including:

  1. mod_rewrite - the problem with this approach is that it becomes a huge .htaccess file since we need to write a rule for each category.

  2. RewriteMap - this came pretty close since I could query the database to build map file for output. However, I've since learned we don't have access to httpd.conf.

  3. index.php - I've tried running everything through our index.php file which works, but doesn't keep the URL in the browser friendly for SEO purposes.

I'm hoping somebody has another idea which might help, I'd really appreciate it. If you've got a reference to something on the web that would help that'd be great too.

Why not to route all requests to the index.php with mod_rewrite and use PHP to write the routing logic, which seems way more reliable way than writing distinct rewrite rules?

As simple .htaccess as this one

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

And few lines of PHP code in the index.php

$client_name = strtok($_SERVER['HTTP_HOST'],".");
list ($cat,$subcat) = explode("/",trim($_GET['request'],"/"));

除了Shrapnel上校的回答之外,至少要减少选择的范围:

RewriteRule ^/([-a-zA-Z0-9_]+)/([0-9]+)$ index.php?function_name=$1&cat=$2 [L,QSA]  # domain.com/cat_name/23 is sent to the server as domain.com/index.php?function_name=cat_name&cat=23

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