I have the following excerpt in my .htaccess file:
RewriteEngine On
# Whether RewriteBase is commented does not appear to have an impact on my problem
# RewriteBase /
RewriteRule ^admin/(.*) members/$1 [L]
The idea is that the pages in /admin load from the /members directory, since certain features are duplicated between them, so that I don't have to duplicate my code. (There are other directives in the file that are related to a CMS system, and those are working as expected.)
On the current Apache 2.2 server, everything works fine. My client's webhost wants to upgrade to a server with Apache 2.4, and on the test server the rewrite rule fails. Instead of loading the page, it just displays "File not found."
All other rules are executed as expected, it's just that one that fails. I have tried adding/removing / characters and removing [L] from the rule, it always fails. I tried adding [R] to send the redirect to the browser, but even that failed. But if I change the url to the same filename but in the members directory, then the file loads successfully.
Being able to change one directory name to another seems like a pretty basic function of mod_rewrite. What am I missing?
Related
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]
I'm using Liferay 6.2 EE that runs on tomcat but it's fronted by an Apache server. I want to redirect users so that whenever they hit the old liferay URL, it redirects them to the new liferay URL. I changed the URL in liferay, so it is now the new URL. However, whenever I try to go to the old URL, I get a page request error. It never redirects me to the new URL. In /san/apache/conf/ I put my redirect code inside of httpd.conf. This my code:
RewriteEngine On
RewriteRule ^group/old/(.*) /group/new/$1 [L]
After I applied these changes, I restarted the Apache server and it still doesn't work. I've tried a bunch of other combinations as well. Does anyone know what I'm doing wrong? Is there some place else I have to make this change?
Ah, since your rewrite rule is lying in the server config file (instead of htaccess file), the mod-rewrite receives URLs with the leading slashes (/). So, the rule should be:
RewriteEngine On
RewriteRule ^/group/old/(.*) /group/new/$1 [L]
OK, I'm looking to use mod rewrite to write seo friendly urls for my site. It works fine on my xampp local testing server. However, I'm having problems when trying to put it live, it doesn't seem to work at all.
Here's what I've written in my htaccess file:
RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9]+)$ category.php?location=$1
So the idea is to change mysite.com/category.php?location=londonto mysite.com/category/london.
Initially I though it may be an issue with the ability to mod rewrite being switched on on GoDaddy's end, so I contacted GoDaddy to see if it that was the case and their response was that: "Mod_rewrite is an Apache Web server module installed on all of our Linux servers by default; it does not have to be installed or enabled. Our Linux hosting accounts support most mod_rewrite functions. Because enabling mod_rewrite is handled at a global level, you do not need to enable it in your httpd.conf file. You only need to add the desired code to the body of your .htaccess file."
And they bascially implied that the error is not with them and with my code. I'm not very experienced with mod rewrites and it seemed to work fine when I wrote it like this on other servers. Is there something special you have to write with godaddy? Am I missing something? Thanks!
Is that all in your htaccess file? What error do you get when visiting a desired URL, is it a 500 Internal Server error?
What I usualy add by default is an If-statement:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^category/([a-zA-Z0-9_-]+)$ category.php?location=$1 [L]
</IfModule>
By the way, note the _- at the end of the charset in the rule, for instance New York has a SEF version which is new-york, category/new-york would fail in your RewriteRule.
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.
I have a rewrite rule of the following form:
RewriteRule ^foo/([a-zA-Z0-9]+)$ foo.php?arg=$1 [qsa,nc]
It takes urls of the form /foo/bar and changes them to /foo.php?arg=bar
It works properly locally, and it works on my old host but I moved to a new host (running ubuntu) and it behaves differently.
On the new host apache notices that there is a foo.php and calls it directly. In other words, urls of the form /foo/bar are seen as /foo.php. If I rename foo.php to foo_junk.php and change the rewrite rule to be
RewriteRule ^foo/([a-zA-Z0-9]+)$ foo_junk.php?arg=$1 [qsa,nc]
Then it all works. So it's not that I didn't enable overrides or that I failed to install mod_rewrite or anything. Rewrites work, they're just being done at a different point in the process of resolving a url than they are locally.
Is there a configuration option for this?
Try disabling MultiViews:
Options -MultiViews