I have a LAMP server configured with DocumentRoot /var/www changed to VirtualDocumentRoot /var/www/%-3/ in the Apache2 config file - this allows me to automatically map /var/www/<subdomain> to <subdomain>.example.com.
I'm currently in the process of learning CakePHP (2.x), with Cake in directory /var/www/foo/; I've noticed that the URLS aren't redirecting properly, and am attempting to resolve this issue; as per http://bit.ly/KmEhHl, I've modified the default .htaccess file to (among other things):
RewriteBase /cake
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
But this raises a 500 Internal Server Error - checking Apache2's error.log and rewrite.log, this is because .htaccess is causing an infinite redirect, and is appending app/webroot to the URI.
From my understanding, the L flag here should indicate 'last rule' - I'm assuming that this error is due to my poor understanding of mod_rewrite, but I'm not sure exactly where this is happening. Would the error be because of some other latent issue with this setup? I'm aware that there have been several questions on this already, and will update if any new information can be found on my end. Any help would be greatly appreciated!
Found an answer!
Full answer courtesy of user 'kostasz' at http://bit.ly/NSDWE2 in comments.
The hint was indeed the infinite redirect - using RewriteBase /cake was not the correct base URL.
To fix this issue, add RewriteBase / to each of the .htaccess files in /, /app/, and /app/webroot/.
Related
until now I have always used htaccess to rewrite URLS in order to have non-SEF url to SEF urls.
Today I am facing a new challenge that honestly, beeing non confident in regular expression, I really don't know how to achieve.
I have a situation where a forum on a website of mine has been update in the following form:
previous link: www.domain.com/forum3/topic/name-of-topic/post/7548
new link: forum.domain.com/Topic-name-of-topic/
How do I intercept /post/37764 string and tell htaccess to not consider it?
And how to instruct the server to build that kind of url instead of the provious. I am very confused about it.
Any suggestion? Thank you very much. Is there any resource that I can read to help me better understand the case?
Thanks again.
EDIT
Florian answer is correct. I just added few mods to fit it better.
RewriteEngine On
#RewriteBase /
RewriteRule ^forum3/topic/([^/]+)/post/[0-9]+$ http://forum.domanin.com/Topic-$1/ [L,R=301]
RewriteRule ^forum3/topic/([^/]+)-[0-9]+$ http://forum.domanin.com/Topic-$1/ [L,R=301]
You can try this code :
RewriteEngine On
RewriteBase /
RewriteRule ^forum3/topic/([^/]+)/post/[0-9]+$ /Topic-$1/ [L,R=301]
/([^/]+)/ means that we want to catch a string containing one or more characters except / preceded and followed by a /.
This link might help you to test your .htaccess files :
Test your apache htaccess files online
I have an .htaccess file with several lines. It does not work as expected. Mod_rewrite is enabled. RewriteLogLevel is set to 9.
The first two rules are there to forbid uris with a length more then 80 characters:
RewriteCond %{REQUEST_URI} ^.{80}
RewriteRule .* - [F]
It does not seem to get evaluated as every test url passes through and it does not generate an error either.
I also tried:
RewriteRule .{80} - [F]
But that did not do the trick either. The process ends with a 404, not a 403.
This next rule is not working either. It used to work.
RewriteRule ^(\/)?([\w]+)$ /index.php [L]
The URI /Contact was always handled by this index.php.
Whatever URL I type I get a 404. I should get a 403 or a 200. Not a 404. What am I missing?
Apache has on all directories the permission to read, write and execute and on all files the permission to read and write.
The two urls for testing are:
127.0.0.4/asssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssddddddddddddddddddddd?p=s&s=psv
and
127.0.0.4/Contact
The alias for 127.0.0.4 used is considerate.lb.
Try this rule instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\S{80}
RewriteRule ^ - [F]
Using THE_REQUEST instead of REQUEST_URI as that variable might get overwritten due to presence of other rules in your .htaccess
Finally I have found a solution. The problem was not in the coding of the .htaccess. I replaced the file with a previous version, added the new lines to test the request and it worked all fine.
It is not a satisfactory solution, because it can happen again and I do not have any clue what caused the error. If someone knows the error, I would love to hear what might have been the exact cause and how to solve that properly. I would like to change the tags of the question as the current tags might be misleading (although other people might experience the same problem how apache handles a .htaccess file), but I do not know which tags I should use.
I'm just going to explain my problem here :
http://mysite.com/movie/20000
should be rewritten to
http://mysite.com/movie.php?id=20000
my htaccess file :
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie/([0-9]+)$ movie.php?id=$1
On my localhost WAMP installation this works fine, but when I put it online on my linux host it doesn't completely work. It does go to the movie.php page, but it seems it gives no GET parameter id.
Edit :
if I use
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie([0-9]+)$ movie.php?id=$1
then
http://mysite.com/movie20000
Goes to the correct page, but I would like /movie/20000 and not just /movie20000
It also seems that my host automatically rewrites a visit to mysite.com/movie to mysite.com/movie.php
After searching for a long time, and pulling some of my lovely hair out I found the solution.
I just added
Options -MultiViews
to my htaccess file and that fixed it.
Why? Because .php was being added to urls without an extension, which I really did not need.
This should work.
RewriteRule ^movie/([0-9]+)$ http://mysite.com/movie.php?id=$1 [NC,L]
Don't forget the [NC, L] it means Case insensitive, last rule... If you don't, it will continue to process through your htaccess rules, maybe triggering other ones.
Still, the stuff below is good advice.... :)
Check to see if the Rewrite module is loading with apache. Look for this line in the httpd.conf file:
LoadModule rewrite_module modules/mod_rewrite.so
Check to see if your apache config allows for .htaccess files for your system or in the virtual host definition.
Also, test with a simpler rewrite catch all and test that alone to see if it's working at all like this (get rid of everything else in your htaccess file to limit the test):
RewriteEngine On
RewriteRule ^(.*)$ http://www.google.com [L,R=301]
Any request to your site should go to google if the configuration for apache is correctly set.
we moved our joomla site and rebuilt. in the process a link got moved that we need to be as it was before.
before:
www.mysite.org/kindergym
now it lives here:
www.mysite.org/education/kindergym
it would seem that it would be easy to go into com_redirect and do this. however, it only works for the following
mysite.org/kindergym without the www
with the www attached writing the old url returns a 404 error page, not a redirect.
i tried to make a separate redirect with the www too and it wouldnt let me. i tried a separate module with no success and have played around with the .htaccess file (although i am not very knowledgeable about htaccess).
could someone explain the reason why this would be an issue? the difference between the two. i tried calling my host and they were less than helpful and actually told me what i wanted to do couldnt be done LOL.
thanks.
I take it the solution you have would work if you redirect the entire mysite.org to www.mysite.org?
If so, create a .htaccess file in the website root. Put the following inside it:
########## Begin - Redirecting non-www request to www
#
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.org [NC]
RewriteRule (.*) http://www.mysite.org/$1 [L,R=301]
#
########## End - Redirecting non-www request to www
You also need to make sure mod_rewrite is enabled on the apache-server, but I think most providers support that.
I suggest you post your full .htaccess here. However I think all you need is this rule:
RewriteRule ^(?!education/).*)$ education/$1 [L,NC]
The other two answers are good! but better implement 301 redirect in httpd.conf since it's compiled once on server restart. The same code in .htccess is interpreted for each and every HTTP request!
I'm working on my project and now I want to transfer it from my localhost to server. Everything seems to work fine, but .htaccess doesn't work.
The server should be suporting mod_rewrite and print_r(apache_get_modules()); shows mod_rewrite running.
But still, when I type myaddress.com/contact, it shows error 404.
Here is the .htaccess file, but it should be fine, since it runs on my localhost:
RewriteEngine on
RewriteRule ^adminator/?$ adminator/login.php [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&detail=$2 [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
Do you have any idea?
Thanks, Mike.
Does the server allow .htaccess? It doesn't really have to.
Try other directives, ask your hosting provider, or check the configuration yourself if possible.
I tried testing your rules they should be fine on urls such as: http://myhost.com/test1 and http://myhost.com/test1/test2 result in requesting url: index.php?page=test1 and index.php?page=test1&detail=test2
So the problem is not your rules, it is your server setup somewhere. As Mewp said check your Apache config file, look for AllowOverride which should be enabled for .htaccess files to overriding default settings.
Ok, fixed it .. deep in the hostings settings, there was an option to activate it .. but I found it after 4 hours of looking around .. :P