Little help with rewrite

Discussion in 'General OpenLiteSpeed Discussion' started by stdio, Mar 23, 2017.

  1. stdio

    stdio New Member


    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: 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?
  2. lsfoo

    lsfoo Administrator

    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: redirects to, which may not be what you're looking for.

  3. stdio

    stdio New Member

    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:

    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.

  4. lsfoo

    lsfoo Administrator

    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/?
  5. stdio

    stdio New Member

    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.
  6. lsfoo

    lsfoo Administrator

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

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

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

    stdio New Member

    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!
  8. lsfoo

    lsfoo Administrator

    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!
  9. stdio

    stdio New Member


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

    2017-04-03 18:35:48.040 [INFO] [] [REWRITE] Rule: Match '/rd/google' with pattern '^/index\.php$', result: -1
    2017-04-03 18:35:48.040 [INFO] [] [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:

    Thank you!
  10. lsfoo

    lsfoo Administrator

    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:

    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. stdio

    stdio New Member

    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. lsfoo

    lsfoo Administrator


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


Share This Page