0

I've been searching but I can't quite find what I'm after. I'm trying to find an elegant way to redirect my url path to the query string via htaccess and mod_rewrite.

The problem is that the path does not have a fixed amound of sub dirs and could be huge.

i.e. http://example.com/sub1/sub2/sub3/sub4/sub5/sub6/sub7...

Currently I just have a load of rules in my htaccess to capture these...

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/? /index.php?sub[]=$1&sub[]=$2&sub[]=$3&sub[]=$4&sub[]=$5 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/? /index.php?sub[]=$1&sub[]=$2&sub[]=$3&sub[]=$4 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? /index.php?sub[]=$1&sub[]=$2&sub[]=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/? /index.php?sub[]=$1&sub[]=$2 [QSA,L]
RewriteRule ^([^/]+)/? /index.php?sub[]=$1 [QSA,L]

... but there may be more, anyone know of a way to autoamtically parse these in to the query string?

2
  • 1
    This would be a lot easier, if you just passed any path to your script, and then exploded that path by / within your script … Commented Feb 24, 2015 at 15:04
  • Thanks, I've tried this already with something like this but I was wondering if it could all be handled in htaccess RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/]+)/(.+)$ /index.php?module=$1&cats=$2 [QSA,L] Commented Feb 24, 2015 at 16:15

1 Answer 1

2

If the paramater name is always the same, you could just rely on the rewrite engine to loop through them all:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?sub[]=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.+)$ /$2?sub[]=$1 [L,QSA]
Sign up to request clarification or add additional context in comments.

3 Comments

sorry but I'm not quite following that, I just get a 404 when trying your example
@MattJenkins it does exactly what you are asking in your question for me: i.sstatic.net/tUxk8.png i.sstatic.net/tUxk8.png
ah, ok yeah I've got it now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.