VSG24 VSG24 - 1 month ago 7
PHP Question

mod_rewrite issues and php

I'm trying to convert my app links, so that a link like this:

http://localhost/index.php?id=13&category=Uncategorized&title=just-a-link


gets converted to this:

http://localhost/13/Uncategorized/just-a-test


so far I was able to do it using:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]


but that completely breaks links to css and other files as it redirects every request with query to index.php

so I changed it slightly so that it only runs when the first query is a number:

RewriteEngine On
RewriteRule ^([0-9]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]


this one doesn't break css and js files, however when you go to the link for example
http://localhost/13/cat/test
then try to go to another link like
http://localhost/19/Uncategorized/something
by clicking on it inside the previous page it will instead redirect you to something like
http://localhost/13/Uncategorized/19/Uncategorized/just-a-test


How do I achieve what I described without any of these weird side effects??

Answer

Use :

RewriteEngine On
RewriteRule ^([0-9]+)/([^/.]+)/([^/.]+)/?$ /index.php?id=$1&category=$2&title=$3 [QSA,L]

And add this code to your html <header>:

<base href="/">

OR

<base href="http://www.domain.com/">

To fix relative css and js links

Comments