mod_rewrite VS relative paths

前端 未结 5 2126
耶瑟儿~
耶瑟儿~ 2021-02-06 14:03

My mod_rewrite code is:

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
Rewrite         


        
相关标签:
5条回答
  • 2021-02-06 14:08

    Adding the line:

    <BASE href="http://www.sitename.com/">
    

    to my page inside the <head> tag works great for me. It can be coded even a PHP function to automatize this so you don't have to bother with copy/paste on other occasions.

    Update:

    <?php
    $BaseDir = 'http://'.$_SERVER['SERVER_NAME'].substr($_SERVER["SCRIPT_NAME"], 0, strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
    ?>
    
    <head>
    ...
    <BASE href="<?php echo $BaseDir; ?>">
    ...
    </head>
    
    0 讨论(0)
  • 2021-02-06 14:11

    you should always use absolute paths for images css etc, when playing with mod_rewrite but simple solution might be to ignore css,js, images and rest in RewriteRule ^(.+)?$ index.php?url=$1 [L,NC]

    RewriteRule ((js|css|images)\/.*)$ /$1 [L,NC]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+)?$ index.php?url=$1 [L,NC]
    

    however this might not work if these dirs actually depend of the current url

    0 讨论(0)
  • 2021-02-06 14:15

    I've tested the tag and there's no need to indicate http:// The following code worked for me:

    <base href="<?php echo dirname($_SERVER['PHP_SELF']);?>/" />
    
    0 讨论(0)
  • 2021-02-06 14:24

    The server won't ever know whtether the request came via relative or absolute path so there is nothing You can do about it. As stated in another answer, You could make exceptions but maintaining all of them etc will become PITA at some point. ;)

    I always use absolute paths especially when rewriting so I'd suggest just that, it's the easiest way

    0 讨论(0)
  • 2021-02-06 14:25

    Add a <base href="yoururl.com/beta/"; /> tag to your HTML head. Also add RewriteBase /beta/ to the htaccess if you're in that subfolder

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