I have trouble rewriting virtual directories as variables.. can someone help please?
The URL is:
https://www.example.com/content/index.php?var=1-dKein4i59xcNjfks
I want it to look like:
https://www.example.com/content/featured/1-dKein4i59xcNjfks
where "content" is a real directory and "featured" and "1-dKein4i59xcNjfks" are virtual
my current .htaccess code is incorrect as:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} content/index.php?var=([^& ]+)
RewriteRule ^content/?index.php$ /var/%1? [L,R=301]
Can someone assist please? thanks
2
Answers
With your shown samples please try following. Make sure to clean your browser cache before testing.
rewrite your URL so it looks cleaner, you want to change
index.php?var=...
tofeatured/...
in the URL, while still lettingindex.php
process the request. Here’s an updated.htaccess
code for that:How works:
First
RewriteCond
andRewriteRule
: Captures the var value fromindex.php?var=...
and redirects it to/content/featured/...
to give a cleaner URL.Second
RewriteRule
: When someone visits/content/featured/...
, it internally rewrites the URL back toindex.php?var=...
, soindex.php
can handle it as if the original query was still there.Now, visiting https
://www.example.com/content/index.php?var=1-dKein4i59xcNjfks will look like https://www.example.com/content/featured/1-dKein4i59xcNjfks
without breaking functionality.