nick
nick

Reputation: 3227

Mod_rewrite not working on subdirectory on localhost

I have this folder system:

localhost/websites/2018/mywebsite

And there I have this file:

products.php

so the full route I type in my browser to access it is:

localhost/websites/2018/mywebsite/products.php

Now, I'm trying to use mod_rewrite to get this:

products.php?cat1=a&cat2=b&cat3=c

into this:

shop/a/b/c

So the full url would be:

localhost/websites/2018/mywebsite/shop/a/b/c

I tried putting the .htaccess in here:

localhost/websites/2018/mywebsite

with this code:

RewriteEngine On
RewriteRule ^shop/([^/]*)/([^/]*)/([^/]*)$ /products.php?cat1=$1&cat2=$2&cat3=$3 [L]

But when I go to

localhost/websites/2018/mywebsite/shop/

I get a 404. I think it has to do with the directory tree because when I upload it to the website (root directory) it works fine.

Edit: I tried using

RewriteBase localhost/websites/2018/mywebsite

And some variations but it didn't work. It threw 500 server error.

Upvotes: 0

Views: 120

Answers (1)

Joe
Joe

Reputation: 4907

You would need to use %{QUERY_STRING} for this to work. Try something like the following:

RewriteEngine On
RewriteCond %{QUERY_STRING} cat1=(.+)&cat2=(.+)&cat3=(.+)$ [NC]
RewriteRule ^ /shop/%1/%2/%3/? [R=301,L,NE]

This grabs each of your variables and then rewrites /shop/a/b/c onto the end of your URL. The use of ? is there to stop the original query from appearing on the end of the URL.

Make sure you clear your cache before testing this.

Upvotes: 1

Related Questions