Search code examples
apachemod-rewriteurl-shortener

Apache htaccess rule for url shortening


I'm trying to create something short.com/ASWi7 -> short.com/index.php?h=ASWi7

so I tried using this code on my htaccess

RewriteEngine on
RewriteCond %{REQUEST_URI}  !^/index.php
RewriteRule ^(.*)$ /index.php?hash=$1   [L]

but the website loses the css and everything...i organized my site in folder etc... so how can i exclude all the folders or cetain files to be part of that rule?


Solution

  • Try the following:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI}  !^/index.php
    RewriteRule ^(.*)$ /index.php?hash=$1   [L]
    

    !-f checks for existing files

    !-d checks for existing directories

    Hope that helps.