user1211577
user1211577

Reputation:

.htaccess Rewrite without index.php

I have a URL with the structure http://localhost/slugone/dynamicslug?query=00000, which I am trying to rewrite to http://localhost/slugone/dynamicslug/00000.

I've managed to get .htaccess to recognise this RewriteRule

RewriteRule ^slugone/(.*)/(.*)$ /slugone/$1?query=$2 [L,R=301]

But this doesn't do what I need it to, instead of masking /slugone/dynamicslug/00000 to be /slugone/dynamicslug?query=00000, it is instead redirecting the pretty URL to the URL with query parameters.

/dynamicslug/ is a slug that is used to show a specific product on the page, and the ?query=00000 is used to select a variant of this product, so I can't explicitly use /dynamicslug/ in my rewrite rule, either.

Searching SO hasn't given me any results, as all of the questions I can find are using index.php in their rewrite, which I am not.

Upvotes: 1

Views: 445

Answers (1)

Florian Lemaitre
Florian Lemaitre

Reputation: 5748

You should reverse your RewriteRule:

RewriteCond %{QUERY_STRING} ^query=([0-9]+)$
RewriteRule ^slugone/([^/]+)$ /slugone/$1/%1? [L,R=301]

EDIT:

If you want it to behave as a pretty URL you should only remove the redirection in your initial RewriteRule:

RewriteRule ^slugone/(.*)/(.*)$ /slugone/$1?query=$2 [L]

Upvotes: 2

Related Questions