Apache rewrite - Virtual dir to php script - apache

Following the accepted answer to this post, I constructed a rewrite rule which is pretty straight forward, but doesn't seem to work as designed.
# /st doesn't exist, it's virtual
example.com/st/sometext
Rewrites to
# /app is a symlink to /app.php, and this link works if entered directly
example.com/app/st/sometext
And the rewrite rule:
RewriteRule ^st/(.*)$ app/st/$1 [L]
This rule is at the top of the rewrite block, after which there are other rules, but those rules shouldn't apply, due to the [L] constraint.
The rewrite log file has this:
(2) init rewrite engine with requested uri /st/sometext
(1) pass through /st/sometext
It doesn't seem like the rewrite rule is matching the url. What am I missing?

Ok, I resolved the issue by switching over to nginx (lol) and implementing this rewrite rule:
location /st {
rewrite ^/st/(.*)$ /app.php/st/$1 last;
}
The switchover to nginx really had little to do with the rewrite rule and more to do with getting away from apache 2.2 (default upstream version for centos), however with nginx, rewrite rules are more straight forward than apache's (imo).

Related

Redirect and Rewrite Rule for Apache

I am trying to install a CalDAV client on my apache webserver, and I am having trouble using a combination of redirects and rewrite rules to get a desired url.
The document root for my webserver is /var/www, and the calendar files are stored in /var/www/agendav/web/public. If I go in through my browser at <website>/agendav/web/public/index.php, I have no trouble getting to the interface and using it, so that is not a problem. However, my desired URL for the calendar is <website>/calendar/, instead of having to go down through the agendav folder tree. I have been trying to perform this with a redirect rule, and a rewrite rule, but to very little success. I have found a few other answers here that have gotten me close, such as this one and this one, with a working redirect, but I am still having issues with the rewrite rule. Here is the current solution I have:
# Rules for the Calendar
Redirect "/calendar" "/agendav/web/public"
RewriteEngine On
RewriteCond %{REQUEST_URI} "^/agendav"
RewriteRule "^/agendav/web/public/(.*?)$" "/calendar/$1"
My current solution seems a little circular. First, I redirect the user to the agendav folder, then then try to hide the redirect behind a rewrite rule, when it seems that I could just get away with a single rewrite rule. Unfortunately the group I am working for is not big enough to have their own dedicated server manager, and I ended up with the job despite knowing very little about it. Any help to get this would be greatly appreciated.
You don't need the redirect, you can do it with just a rewrite rule, the problem was that you have the condition and the rule reversed, you were using the real path for the conditions instead of the "virtual" path:
RewriteEngine on
RewriteCond %{REQUEST_URI} "^/calendar" [NC]
RewriteRule "^calendar/(.*)" "/agendav/web/public/$1"
With this rules all requests for http://yourwebsite/calendar/* are internally served with http://yourwebsite/agendav/web/public/*.

Apache %{REQUEST_URI} not working correctly

I am not using Virtual Hosts or anything fancy though I have some .htaccess files setup. Following is my rewrite rule in httpd.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/app/smsapi [NC]
RewriteRule (.*) https://www.example.com/uri=%{REQUEST_URI} [R,L]
This rule basically says that if the uri does not begin with /app/smsapi then fire the rewrite. But when I restart the server and try it I get some weird results.
When I request the URL https://www.example.com/app/smsapi/index.php, I get a 200 Success code which is as expected. But, when I request the URL http://www.example.com/app/smsapi/index.php, it redirects to https://www.example.com/uri=/app/smsapi/index.php. So it actually fires the rule even though the request URI does not satisfy the condition.
So, then I decided to turn off the rewrite rules and give it a go. Now, both those URL give me a 200 Success code.
Now, I know this problem cannot be solved easily by other people who do not have access to the server, but am I right in saying that this is certainly a problem with REQUEST_URI not firing correctly? I have shown that without the rewrite rule, everything works normally, but with the rewrite rule, the second URL is redirected. Therefore, the redirection must be caused by the rewrite rule? Additionally, the condition for redirect rule is not satisfied. Doesn't this prove that there is something wrong with the functioning of the rewrite rule?
Is there any other possibility?
UPDATE
Something very weird is happening here. I setup a local server and tried the same rule and what I got for the URL http://192.168.0.112/app/ is
http://192.168.0.112/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/app/
which is correct because as long as the URL is not like /app/smsapi, it should redirect it. Wonder why this is not happening on the real server. Also, where you insert these rules seems to make a difference. (I am only including these rules after the LoadModule command).
On localhost, if I put these rules either above or below the Directory section, it won't work. But, if I include it inside the Directory section it will.
On server, if I include the rules inside the Directory section, they won't work. But, if I include them either above or below the Directory section, they start working.
This seems to me to be due to a difference in the versions. My localhost is an Ubuntu Desktop 16.04 running Apache 2.4.18. While the server is CentOS 6.8 running Apache 2.2.15.
But, i think the mystery as to why on the server redirect happens only once (though it is configured to go upto 20 times) has something to do with https. Which is also related to the original problem in which https is redirected even on a non-matching rule.
Clues anyone?
UPDATE
I updated the httpd.conf file with the same rules but I used http:// instead of https:// and it gave me the correct result with 20 redirects. That means I have isolated the problem to https.
You are reporting the exact issue in the first phrase: "I am not using Virtual Hosts or anything fancy though I have some .htaccess files setup"
.htaccess is "fancy" and overcomplicated, not virtualhosts.
If you had defined that RewriteCond in virtualhost in the first place it would work, but .htaccess is per-dir context (aka a nightmare) and the regex ^/ will never match in that context.
If you want to match REQUEST_URI in per-dir context (directory or .htaccess) you need to drop the initial slash, that is:
RewriteCond %{REQUEST_URI} !^app/smsapi [NC]
Extra, also consider you MAY NOT need to add a RewriteCond for this:
RewriteRule ^(?!app/smsapi)(.*) https://www.example.com/uri=$1 [R,L]

mod rewrite clear home url for tomcat + apache

I have a struts app running on Apache front end of Tomcat via mod-jk.
I am trying to use mod-rewrite to clean some of the action urls generate by struts.
Example: rewrite (works fine)
http://www.demo.com/context/user.do?action=aboutus to http://www.demo.com/context/aboutus using
RewriteRule ^/msn/aboutus$ /msn/user.do?action=aboutus [PT,L].
Problem: I'd like to rewrite the http://www.demo.com/context/user.do?action=home to http://www.demo.com (homepage)
I tried this
RewriteRule ^/$ /context/userdo?action=home [PT,L] which does not work.
FYI
All the css,js and links are on relative path.
DirectoryIndex is on index.html (Does it change mod-rewrite behaviour?)
Tomcat Version 5.5, Application deploy via exploding the WAR file in public_html/context/ folder (multiple deployment)
URLrewrite filter does not help to remove the context name according to this.
Logs
I tired to have a look at the log file (snapshots) which doesn't give any warning error messages.
(3) applying pattern '^/$' to uri '/context/jsps/images/abc.png'
(3) applying pattern '^/abc/aboutus$' to uri '/abc/jsps/images/abc.png'
(3) applying pattern '^/abc/home$' to uri
'/abc/jsps/images/abc.png'
Anyone can give me some ideas what went wrong? and how can I solve that issue?
I think in your case of rewriting ^/$ it is mod_jk that is taking precedence over mod_rewrite rules since using mod_jk you are forwarding everything eg: /* to Tomcat from Apache. I will suggest using URLRewriteFilter within your Tomcat app to achieve these URL rewrites properly.

apache .htaccess file on lighttpd

RewriteEngine on
RewriteRule ^packed\.js$ pack.php?debug=0 [nc]
RewriteRule ^debug$ pack.php?debug=1 [nc]
That worked fine on apache in a .htaccess file placed in a specific directory. If I want to do this on lighttpd, do I have to add it in the config file or something?
Would I need to make any changes to these rules?
lighttpd doesn't support .htaccess files like Apache httpd does. That's where the "light" in "lighttpd" comes into play.
You can, however, migrate these rules from Apache httpd's mod_rewrite to lighttpd's mod_rewrite. But be aware that the NC flag (case-insensitive matching) is not supported by lighttpd's mod_rewrite. If you are fine without it, you could simply use the following rewrite rules:
url.rewrite-once = (
"^packed\.js$" => "pack.php?debug=0",
"^debug$" => "pack.php?debug=1"
)
If you need the match to be case-insensitive, you'll probably need to invoke mod_magnet and a custom Lua script.

How to configure apache (ubuntu) such that www.mysite.com will direct to www.mysite.com/drupal6/?

I am a newbie to ubuntu and apache. Can someone tell me how I could direct to
www.mysite.com/drupal6
when user address www.mysite.com?
Thanks a lot.
Cheers.
If you are running Apache and Ubuntu, there is actually a really easy way to force this redirect using a simple php script.
Create an index.php file in the root of your server and paste the following code into it
<?php header("location: drupal6/") ?>
This will cause the site to auto-redirect to the drupal6 folder whenever it is visited.
This should work. Create a file in the root folder of your server called .htaccess - the dot at the beginning is very important as this helps the server identify the file as a hidden / system config file.
Open the file and paste the following lines of code in :
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ www.mysite.com/drupal6/$1 [R,L]
This should force all traffic to the server to redirect to your custom folder.
A brief explanation of the .htaccess code
If you want rewrites to work, you have to enable the Rewrite Engine and tell the server to follow symlinks.
The second section establishes the rule - specifically applying it to all traffic on the standard web port of 80.
The final line tells the server to grab everything after the URL and append it to the new address (mysite.com/drupal6).
There's a lot more you can do with .htaccess files but you really need to Google for good examples to test out.
Look at Apache's mod_rewrite documentation. You will need a RewriteRule in your apache configuration at the minimum, you may also need RewriteCond's to define when the RewriteRule is used.
Your rewrite pattern will be rewriting the REQUEST_URI with something from: ^/$ to: /drupal6. The ^ and $ are essential to prevent Apache getting into an infinite loop while rewriting the base URI by only matching "/" and not "/anything-else".
I assume you're on a recent version of Ubuntu and Apache? If so, see the Apache 2.2 documentation on mod_rewrite.