开发者

Using mod_rewrite to redirect 70+ URLs with variables

开发者 https://www.devze.com 2022-12-09 14:28 出处:网络
I am constructing a webcomic site, but the chapter order has changed significantly. I have manually written the conversions for myself in a form like this:

I am constructing a webcomic site, but the chapter order has changed significantly. I have manually written the conversions for myself in a form like this:

36 -> 26.1

37 -> 28

38 -> 28.1

39 -> 29

40 -> 30

41 -> 30.1

Basically, following this guide, I want to convert all urls like http://www.domain.com/view.php?chapter=38 to the newer kind like http://www.domain.com/c28.1.

I also want to ensure that those requesting a page 开发者_运维百科number, like view.php?chapter=38&page=4 have their page numbers passed on to the redirect, like so: c28.1/p4.html.

It would seem pretty simple, except that I have 70 or so of these to implement, and am a little worried if this significantly hurts site performance (is 70 a lot..?) and can't get the comic and page variables to properly rewrite.

So my question is, how do I achieve this, what is my best solution? If performance is no problem, I would prefer to keep it in the .htaccess with my other mod_rewrite code, but if the only reasonable way to do it is write a .php script and call on it to do the redirect, I can do that - though I don't really know how to get PHP to do that.

Please keep in mind that the 70 or so mentioned are the only ones I will ever have, so I don't care to make an easily-accessible database. I just want the redirects to work with minimal fuss.

I will be so grateful for any response. Thanks in advance if anyone can help me figure this one out.


I think a lighter way (in terms of rows inside .htaccess file) to approach is, like you have mentioned, to use .htaccess to handle urls like:

RewriteRule ^c([0-9]+)$ view.php?chapter=$1  
RewriteRule ^c([0-9]+).([0-9]+)$ view.php?chapter=$1&section=$2  
RewriteRule ^c([0-9]+).([0-9]+)/p([0-9]+).html$ view.php?chapter=$1&section=$2&page=$3

And then map them to a php script that will handle the single contents.

I can suggest you that you should have also to map by rewriterule with a 301 redirect all the old urls in order to allow people that are coming from a search engine serp to be redirected to the right new url of the content.

Hope this helps.


I ended up consulting with the wonderful people of freenode #httpd and got a solution. For me, as I only want to catch requests for view.php, I found that it worked to simply write a view.php script that gathered things from an array. No mod_rewrite necessary. The script I am now using looks like:

<?

$chapters = array(
    // old => new
    1   => 2,
    2   => 3,
    3   => 4,
    10  => 21
);

if (! isset($_GET['chapter']) || ! isset($chapters[$chapter = (int)$_GET['chapter']])) {
    header('HTTP/1.1 404 Not Found');
    echo 'Page Not Found';
    exit;
}

$url = '/c' . $chapters[$chapter];
if (isset($_GET['page']))
    $url .= '/p' . $_GET['page'] . '.html';

header('Location: ' . $url, 301);

?>

Thanks for your help, though, gh3.

0

精彩评论

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