PHP + HTACCESS + mod_rewrite + different length url segments

本秂侑毒 提交于 2019-12-04 17:17:24

You can cater for different numbers of matches like this:

RewriteRule ^/([^/])* /content.php?part1=$1 [L,QSA,NC]  
RewriteRule ^/([^/])*/([^/])* /content.php?part1=$1&part2=$2 [L,QSA,NC] 
RewriteRule ^/([^/])*/([^/])/([^/])* /content.php?part1=$1&part2=$2&part3=$3 [L,QSA,NC] 

Where [ ^ / ] to matches any character other than '/' - and then because that term was enclosed in () brackets, it can be used in the re-written URL.

QSA would handle all the parameters and correctly attach them to the re-written URL.

How you match up the parts with things that you know about is up to you but I imagine that something like this would be sensible:

$knownKnodes = array(
    'about',
    'projects',
    'news',
    'join',
);

$knownSubNodes = array(
    'the-team',
    'project-x',
    'the-team'
);

$node = FALSE;
$subNode = FALSE;
$seoLinks = array();

if(isset($part1) == TRUE){
    if(in_array($part1, $knownNodes) == TRUE){
        $node = $part1;
    }
    else{
        $seoLinks[] = $part1;
    }
}

if(isset($part2) == TRUE){
    if(in_array($part2, $knownSubNodes) == TRUE){
        $subNode = $part2;
    }
    else{
        $seoLinks[] = $part2;
    }
}

if(isset($part3) == TRUE){
     $seoLinks[] = $part3;
}
if(isset($part4) == TRUE){
     $seoLinks[] = $part4;
}

Obviously the list of nodes and subNodes could be pulled from a DB rather than being hard-coded. The exact details of how you match up the known things with the free text is really up to you.

in wich structure does the php script get the information?

if the structure for 'example.com/news/article/new-article' is

$_GET[a]=news
$_GET[b]=article
$_GET[c]=new-article

you could check if $_GET[c] is empty; if not the real site is $_GET[b], and so one...

an other way is that $_GET[a] will return someting like 'news_article_new-article' in this case you have an unique name for DB-search

I hope I understood you right

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!