0

In the root folder of my project there are several other folders I am calling "apps". The path to one of these apps is something like: /root_folder/myapp/...

There are actually a dozen apps like this. So I want my project to be using friendly URLs, and to achieve that I am using the apache module mod_rewrite.

I added the following rule to my .htaccess (that is in root_folder, just so you to know) RewriteRule ([^/]*)/(.*)?$ myDomain.com/$1/index.php?params=$2 [NC,L]

So an URL such as mydomain/myApp/param/value/param2/value would be translated to mydomain/myApp/index.php?params=value/param2/value

I performed some tests and saw it working until I added the $1 to refer to the app folder (have a look: the_path_to_my_root_folder_here/$1/index.php?params=$2) The path to one of these apps is something like: `/root_folder/myapp/...** It is generating an URL like: myDomain.com/myApp/index.php?params=index.php

Well I thought it would be a recursion issue. So it seems that Apache will try another redirection after the first is performed, and then it will generate an URL like that

I found this thread in Stack Overflow The problem with the answer is that it`s assuming I know when to stop.

Do you know how to make this second redirection to stop?

I am trying the following rule now RewriteRule myDomain.com/([A-Za-z0-9-]+)/(.*)$/ myDomain.com/$1/index.php?params=$2 [NC,L], but it's not working properly. It is only matching the regex, if I do not pass parameters after the app name. so when I try myDomain.com/user/ it works (not receiving parameters), and fails when I try myDomain.com/user/products/1000/, for example. Instead of rewriting/redirecting, it is trying to find a folder products inside user and etc

2 Answers 2

1

Try changing your rewrite rule to something that will match the input url but not your output url. Something like this:

RewriteRule ^([^\/]+)\/([a-z]+)\/(.+)$ /$1/index.php?params=$2/$3
Sign up to request clarification or add additional context in comments.

2 Comments

hey Stephanie! it works ok, but not sure what you mean with $2 and $3. what if I am passing 2 or 3 'arguments', like mydomain.com/list/user/1000/limit/20 in this URL I am calling the app 'list' and passing user/1000/limit/20 as parameter to be split and parsed in the script. The best idea would be separate it only in two, like app&params and then call app passing params. any ideas?
Instead of capturing all your parameters together, you split them up so that $2 will capture your first paramater name and $3 will capture the first parameter value plus any other parameters you might have. This way your rewrite rule is more specific and will not end up matching your index.php url again.
0

Place this rule in DOCUMET_ROOT/.htaccess:

RewriteEngine On

RewriteRule ^([^/]+)/(.+)$ /$1/index.php?params=$2 [L,QSA]

Unable to understand what you mean by the_path_to_my_root_folder_here

12 Comments

I meant my root folder URL. I will edit my original post
so I tried this and it actually made things worse. now the recursion is not stoping. it is filling the whole address bar with &params=index.php
Can you tell what is location of .htaccess file?
the root is something like myProjectName/www/ so my .htaccess is in this 'www' folder. APPs are also inside www, like myProjectName/www/myApp/
ok thanks and the URL you're entering is this: http://mydomain/myApp/param/value/param2/value2? Also is there any .htaccess in myProjectName/www/myApp/ also? Lastly if there are more rules in myProjectName/www/.htacccess then provide them in your question.
|

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.