Little help with rewrite

#1
Hi,

I just switched from nginx to OLS and I'm experiencing some problems with rewrites in another folder. I think this needs to be done with context but I don't know how to do it.

.htaccess rules look like this:

RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]

This should be placed in a folder - for example /redirect. Basically this (the index.php) loads a text file and redirects stuff to another url. Example: domain.com/redirect/friendly-name will be redirected to a URL set up in the text file.

I did this easily in nginx but I am stuck here. Can anyone help please?
 

lsfoo

Moderator
#2
Hi @stdio

Sorry for such a late reply, we were away the past week at a conference.

OLS does not have full .htaccess functionality. You can include a rewrite file in the rewrite section of the vhost/context configurations and it will load the rewrite rules, but the server will have to be restarted every change.

Regarding the specific rewrite rules you're looking for, a useful feature in OLS is rewrite logging. If you set rewrite logging to 9 and restart the server, you can see in your error log if and how the request is rewritten.

From what I understand of your current ruleset, I see:
1. Enable rewrite
2. If the request is for index.php, do not change anything and end the rewrite parsing.
3. Otherwise rewrite the request to ./index.php?id=request and end the rewrite parsing.

I believe that last rule might be what's causing issues. First thing I noticed was that you do not need a . in front of /index.php. If you have some other exact examples of what you're looking to do, I can help you further. I believe what you currently have will do:
domain.com/redirect/friendly-name redirects to domain.com/index.php?id=redirect/friendly-name, which may not be what you're looking for.

Cheers,
Kevin
 

stdio

New Member
#3
Hi Kevin,

No worries about the late reply. I'm aware about OLS not having full rewrite functionality - that's why I was seeking for help. I managed to set up everything except this simple redirect thing I'm using. While I was waiting for a reply from you, I installed nginx and the rewrite rule looks like this:


Code:
location /rd/ {
  rewrite ^/rd/(.*)$ /rd/index.php?id=$1 last;
}
I pasted this just to help you understand better what this redirect does. I will try and explain better:

Inside /rd I have 2 files. index.php and redirects.txt. index.php reads redirects.txt. The contents in redirects.txt are something like this:

something,something com

Now, what this rewrite does is basically allowing me to access mydomain com/rd/something (friendly url). This can also be accessed like mydomain com/rd/index.php?id=something (non friendly url). I hope this makes sense.

I would like to achieve this in OLS if possible so any help is much appreciated.

Thanks,
Marin
 

lsfoo

Moderator
#4
Hi Marin,

Understood. How do you have OLS configured for that specific situation? Do you have a vhost configured and then a context configured for /rd/?
 

stdio

New Member
#5
Hi Kevin,

I have OLS configured and I also created a vhost and enabled rewrite. I just don't know what rewrite rule to use for this specific situation. Everything else is working perfectly.
 

lsfoo

Moderator
#6
If you do not have a context set up for it, try the following:

Code:
RewriteRule ^/rd/index\.php$ - [L]
RewriteRule ^/rd/(.*) /rd/index.php?id=$1 [L]
In my test, I got:

Code:
2017-04-03 10:57:51.167 [INFO] [192.168.0.215:37950#Example] [REWRITE] Rule: Match '/rd/simpleurl' with pattern '^/rd/index\.php$', result: -1
2017-04-03 10:57:51.167 [INFO] [192.168.0.215:37950#Example] [REWRITE] Rule: Match '/rd/simpleurl' with pattern '^/rd/(.*)', result: 2
2017-04-03 10:57:51.167 [INFO] [192.168.0.215:37950#Example] [REWRITE] Source URI: '/rd/simpleurl' => Result URI: '/rd/index.php?id=simpleurl'
 

stdio

New Member
#7
Hi Kevin,

Thank you for helping me. Can you please advise what kind of context should I choose? I selected "Static" and I set URI to /rd and Location to /rd and added the rewrite rules but it doesn't seem to be working. I get a 404 not found.

Thank you!
 

lsfoo

Moderator
#8
Not a problem!

Static context should be fine. URI should be /rd/, and location should be set to the absolute path (can use $VH_ROOT in place of the vh root part of the path). For the Example virtual host, $VH_ROOT is /usr/local/lsws/Example, and if location should be /usr/local/lsws/Example/html/rd, I would have $VH_ROOT/html/rd.

Let me know if it's still confusing!
 

stdio

New Member
#9
Hi!

Not sure why it's not working for me. The log shows this:

Code:
2017-04-03 18:35:48.040 [INFO] [79.117.29.9:41584:HTTP2-7] [REWRITE] Rule: Match '/rd/google' with pattern '^/index\.php$', result: -1
2017-04-03 18:35:48.040 [INFO] [79.117.29.9:41584:HTTP2-7] [REWRITE] Rule: Match '/rd/google' with pattern 'index.php', result: -1
mydomain com/rd/index.php?id=google works fine.

I made a screenshot of the settings page. You can see it here: http://take.ms/SeHNg.

Thank you!
 
#10
My apologies, that was only for if you did not have a context set up.

As you can see in the logs, it tried to match /rd/google to /index\.php.

Within a context, I believe the rules you're looking for are:

Code:
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*) /rd/index.php?id=$1 [L]
NOTE: The last /rd/ must be kept there, because it's relative to the virtual host, not the context, even in a context rewrite.
 
#11
Thank you Kevin! I was *blind by not seeing "Enable rewrite" was set to "Not set"! After selecting "Yes" it's working! :)

Thank you so much for helping!

Have a great day/evening! :)
 
#12
Awesome!

Remember to turn off logging when everything's working! Let us know if you run into any more issues.

Cheers!
 
Top