Stephen Schmitt
Stephen Schmitt

Reputation: 21

RewriteRule to remove all slashes

Hi I have tried everything and can't find a solution. When a user access this URL

I want it to rewrite it to (not using redirect)

how do I remove the '/' ? Your help is very appreciated .

Upvotes: 1

Views: 89

Answers (2)

Johannes H.
Johannes H.

Reputation: 6167

If that rule should remove any number of slashes, not just a fixed number, you have to create a loop. TO do so, you can use the \[N\] flag, which will cause the rewriting process to start all over with the current URL. Be careful when doing this, this could lead to infinite loops when done wrong ;)

RewriteRule ^/?(.*)/(.*) $1$2 [N]

Should work however. You can test it with this online tool.

[Edit]: Complete rewrite - I forgot about the [N] Flag ;)

Upvotes: 1

Justin Iurman
Justin Iurman

Reputation: 19016

Try with this rule (your htaccess must be located in your document root folder)

RewriteCond %{QUERY_STRING} ^pid=([0-9]+)$
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/([^/]+)\.jpg$ /$1$2$3$4.jpg?pid=%1 [L]

EDIT: this code will only work with urls like in your example (with 3 slashes and pid in query string)

EDIT2: to simplify query string if you have multiple params (maybe in different order) we don't need RewriteCond anymore but QSA flag instead

RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/([^/]+)\.jpg$ /$1$2$3$4.jpg [L,QSA]

Upvotes: 2

Related Questions