.htaccess mod-rewrite regex apache confusion results in 10k 404's per day

前端 未结 1 2038
名媛妹妹
名媛妹妹 2020-12-12 00:50

I have reviewed the many questions posted here related to .htaccess, apache, mod-rewrite and regex, but I\'m just not get

相关标签:
1条回答
  • 2020-12-12 01:12

    I don't know if RewriteMap work with .htaccess files, but anyway here's my solution for virtual host, which should work flawlessly.

    Create a RewriteMap file. See here for more information. This is a very simple text file with: first, the wrong URL without the '/', then one space (at least) and then the right url, like this:

    one-piece-episode-528​ /watch-anime/o/one-piece/​one-piece-episode-528​.html
    dexter-season-6-episode-1 /watch-interesting-stuff/d/dexter/dexter-season-6-episode-1.html
    breaking-bad-full-season-3 /watch-interesting-stuff/b/breaking-bad/​breaking-bad-full-season-3.html
    

    and so on.

    convert this simple text file into hash map. For example:

    httxt2dbm -i mapanime.txt -o mapanime.map
    

    Now declare it in your vhost:

    RewriteMap mapanime \
        dbm:/pathtofile/mapanime.map
    

    So all in all your vhost should look like:

    <VirtualHost *>
        RewriteEngine On
        RewriteMap mapanime \
            dbm:/pathtofile/mapanime.map
        # don't touch the URL, but try to search if it exists in mapanime
        RewriteRule /([^/]*)/$ - [QSA,NC,E=VARANIME:${mapanime:$1|notfound}]
        # if VARANIME not empty *and*
        #   VARANIME different from "notfound":
        RewriteCond %{ENV:VARANIME} ^(notfound|)$
        # then redirect it to the right URL:
        # QSA = query string append
        # R = redirect, 301 = definitive redirect
        # L = last = don't go further
        RewriteRule . %{ENV:VARANIME} [QSA,R=301,L]
    </VirtualHost>
    

    Hope this helps.

    I don't see a simpler solution, but I'm pretty sure this one will work.

    If it doesn't work: read my usual "two hints", and add the rewrite log in your question.

    Two hints:

    Please try to use the RewriteLog directive: it helps you to track down such problems:

    # Trace:
    # (!) file gets big quickly, remove in prod environments:
    RewriteLog "/web/logs/mywebsite.rewrite.log"
    RewriteLogLevel 9
    RewriteEngine On
    

    My favorite tool to check for regexp:

    http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

    0 讨论(0)
提交回复
热议问题