How to hide get variable name from url

P_Star77 :

i've used htaccess for something like this

domain.com/bacaberita.php -> domain.com/baca-berita

and i need passing get varible like this

domain.com/bacaberita.php?id=xy0001

how to hide the variable id so the url going to be domain.com/baca-berita/xyz0001

i'm using htacces code

RewriteRule ^baca-berita bacaberita.php
RewriteRule ^baca-berita/(.*) bacaberita.php?id=$1

but it throws id was empty

i've try this code only

RewriteRule ^baca-berita/(.*) bacaberita.php?id=$1

it work but the css and all images file can not called in the page

Shaun E. Tobias :

You want to put some rewrite conditions before your rewrite rules to ignore those asset files.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/assets
RewriteRule ^baca-berita/(.*) bacaberita.php?id=$1

This tells the server to check that the files or directories exist, then to ignore them in the rewrite if in those directories.


Update

You also want to make sure your assets inside bacaberita.php are linked from the root of the site and not relatively. You can do this by ensuring you always use a / at the beginning:

<!-- incorrect: looks for styles in domain.com/bacaberita/assets -->
<link rel="stylesheet" type="text/css" href="assets/css/styles.css">

<!-- correct: looks for styles in domain.com/assets -->
<link rel="stylesheet" type="text/css" href="/assets/css/styles.css">

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=342622&siteId=1