Question

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?

Was it helpful?

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]

OTHER TIPS

I just improved little bit anubhava reply, this one is based on root folder example, but you can use same approach for other dir that you need.

DirectoryIndex index.php
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  Options -indexes

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule (?!^tip(/.*|)$)^.*$ /index.php [L,NC]
</IfModule>

<IfModule !mod_rewrite.c>
    # Mod_rewrite not installed, redirect all 404 errors to index.php.
    ErrorDocument 404 index.php
</IfModule>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top