regex.htaccessmod-rewritehuman-readable

htaccess human readable


I have a site which already defined htaccess for making url nice

DirectoryIndex index.php
RewriteEngine On
RewriteBase /

Options -indexes

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule (.*) index.php

Everything works fine (there is cms in the root folder).

Now I want to create a folder inside root and make another rules for it:

DirectoryIndex index.php
RewriteEngine On
RewriteBase /

Options -indexes

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !tip [NC]

RewriteRule (.*) index.php [NC]

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^tip/([^/]+)/? /tip/?id=$1

So I want to redirect everything from url: .../tip/?id=N -> ../tip/N

It seems to work fine, id is passed, data is loaded BUT. All the urls are wrong inside site (javascript, css). They aren't loaded.

Look at: http://wincode.org/tip/3

For example, code: <script defer src="js/filtrify.js"></script> produces: http://wincode.org/tip/js/filtrify.js but if you will try to load it in another tab, it will pass js/filtrify.js as id argument, I think. How to fix this?


Solution

  • A. Change your .htaccess code to this in $DOCUMENT_ROOT/.htaccess:

    DirectoryIndex index.php
    RewriteEngine On
    RewriteBase /
    
    Options -indexes
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule (?!^tip(/.*|)$)^.*$ /index.php [L,NC]
    

    B. Change your .htaccess code to this in $DOCUMENT_ROOT/tip/.htaccess:

    DirectoryIndex index.php
    RewriteEngine On
    RewriteBase /tip
    
    Options -indexes
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^([^/]+)/?$ /tip/?id=$1 [L,QSA]