mod_rewrite for all pages (including subfolders) on a site to a single php page

后端 未结 2 1901
闹比i
闹比i 2020-12-19 17:25

I am in the process of converting my site with many static html pages to a site driven by a database. My problem is that I don\'t want to lose what google has already indexe

相关标签:
2条回答
  • 2020-12-19 18:06

    I would recommend you to look into the Apache URL Rewriting Guide, it contains extensive information about rewriting with examples.

    If I understand you correctly, you should be able to use something like this

    RewriteEngine on
    RewriteCond $1 
    RewriteRule ^(.*)$ index.php/?page=$1 [L]
    

    Which is very similar code to the one you posted. If you want better information, be specific about your problem.

    0 讨论(0)
  • 2020-12-19 18:10

    There's no need for so many lines, it only complicates things.

    All you need is 2 lines in .htaccess:

    rewriteengine on
    #rewriterule-: ar1 path =relative. ar2 if relative, that to rewritebase.
    rewriterule !^foo/bar/index\.php$ /foo/bar/index.php
    #..assert ar1 dismatches correct url
    

    In PHP

    You can output the first input of rewriterule in PHP using:

    <?=$_SERVER['REQUEST_URI'];
    

    That will give you all the power and allow you to do all things. Simply parse $_SERVER["REQUEST_URI"] manually and you can echo totally different pages depending on the value of $_SERVER["REQUEST_URI"].


    Sec bugs

    Note that your server may do pathing or buggy pathing before rewriterule. (You can't override this behavior without server privileges.) Eg if the user visits /foo//// you may only see /foo/ or /foo. And eg if the user visits ///foo you may only see /foo. And eg if the user visits /a/../foo you may only see /foo. And eg if the user visits /a//b/../../foo you may only see /foo or /a/foo [because buggy production servers treat multiple / as distinct in the context of .., no kidding].


    With circuit break

    Rewrite circuit breaks on cin identical to htaccess∙parentfolder∙relative interpreted rewriterule∙arg2. (First off, personally I'd disable circuit breaks to reduce rule complexity but there seems to be no way to do so.)

    Circuit-break solution:

    rewriteengine on
    #rewriterule-: ar1 path =relative. ar2 if relative, that to rewritebase.
    rewriterule ^ /foo/bar/index.php
    #..circuit-breaking, so assert ar2 if absolute, htaccess parentfolder =root, else, htaccess parentfolder not in interpreted ar2.
    

    Circuit break and rewritebase undesigned use

    Circuit break needs either of:

    1. arg2 [of rewriterule] ⇌ absolute. and htaccess parentfolder ⇌ root.
    2. arg2 ⇌ relative. and that folder not in interpreted arg2.

    So when that folder ≠ root, circuit break needs arg2 ⇌ relative. when arg2 ⇌ relative, circuit break needs⎾that folder ⇌ not in interpreted arg2⏋.

    Say we need circuit break and a htaccess parentfolder that's in interpreted arg2, so we edit arg2 via rewritebase:

    rewriteengine on
    #rewriterule-: ar1 path =relative. ar2 if relative, that to rewritebase.
    rewriterule ^ bar/index.php
    #..circuit-breaking, so assert ar2 if absolute, htaccess parentfolder =root, else, htaccess parentfolder not in interpreted ar2.
    rewritebase /foo
    #..needs to be absolute [</> starting]. pathing [eg </../a/..> and </../..///ran/dom-f/oobar/./././../../..////////.//.>] allowed
    
    0 讨论(0)
提交回复
热议问题