htaccess RewriteEngine working for HTTP but not HTTPS - .htaccess

I am trying to redirect URLs such as www.mysite.com/foo/bar to www.mysite.com/foo/index.php?foo=bar.
To do this I am using a couple of lines in the .htaccess file, they are as follows:
RewriteEngine On
RewriteRule ^foo/([a-z]+)/?$ foo/index.php?bar=$1 [NC,L]
This works great for connections made over HTTP but I really need this to work over HTTPS as well. When I go to https://www.mysite.com/foo/bar I just get presented with the 404 Not Found error page.
Any help to get this working over HTTPS (or an explanation as to why it cannot be done over HTTPS) would be great. Thanks.
EDIT:
After reading a few comments here is a bit of an update.
I do have an SSL certificate for the site and HTTPS is working fine elsewhere on the site
The document root is the same for both the HTTPS and the HTTP versions of the site. I checked this out using echo $_SERVER['DOCUMENT_ROOT']; - both return the same thing.
I did notice that (when I removed my customised 404 page) that the error shown is: The requested URL document_root/public_html/foo/index.php was not found on this server. I do not have any idea why this might be the case when, if I access the file directly, I can use it fine.
Hopefully this extra info might help a bit.

After some more searching for solutions and looking at examples of .htaccess files I found the solution. I needed to add the following line to my .htaccess file:
RewriteBase /
I am still unsure as to why, without this line, it worked using HTTP but not HTTPS but everything is working well with the addition of this line.
I am no expert in this area but I found a nice explanation of what RewriteBase does here (Line 4): http://randomtype.ca/blog/the-wordpress-htaccess-file-explained/

Related

How do I remove a folder from a URL?

First of all, this question has been asked a few times on stack, however, none of the answers seem to work for me.
I have a website which has a "pages" folder in the root, I want to store all of my website pages in there.
Here's my structure
/pages/folder/folder2/index.php
I want to make it so the link displays:
https://wwww.website.com/folder/folder2/index.php
Removing the "/pages/" part of the URL, when I try all of the answers suggested previously, I get a 404 error.
Here is the htaccess I'm using
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^pages(/.*|)$ $1 [L,NC]
</IfModule>
and i also tried:
RewriteEngine On
RewriteRule ^pages/(.*)$ /$1 [L,R=301]
This htaccess is in the root. I can't seem to get it working, can anyone offer any suggestions? Thank you!
Your second attempt looks fine, though it can be imporoved a bit:
RewriteEngine On
RewriteRule ^/?pages/(.*)$ /$1 [R=301]
That rule should work inside the http servers host configuration or in some dynamic configuration file (".htaccess" style file) if the http server's rewriting module is loaded. You definitely should prefer the first option, but if you really need to use a dynamic configuration file then take care that the interpretation of such files is configured at all and that the file is placed in your hosts's DOCUMENT_ROOT folder and is readable for the http server process.
If that does not work then you need to start debugging. In this case you will start by monitoring your http server's error log file while making test requests. If no hint appears in there you need to enable rewrite logging which allows you to learn what exactly is going on inside the rewriting engine. See the official dpcumentation for the apache rewriting module for that. As typcial for OpenSource software it is of excellent quality and comes with great examples.

my .htaccess is giving me a Forbidden error

I have the following in the root directory of my site:
RewriteEngine on
RewriteRule ^([^./]{3}[^.]*)$ /index.php?page=$1 [QSA,L]
The .htaccess should be redirecting any request 3 chars or longer without . in the string to the index.php/$string-text as to query my CMS db for output, however when i try and load the index page I get a 403 forbidden error... My .htaccess knowledge is quite basic so try and be easy with me! :)
Thanks
Ric
There does not appear to be anything unexpected with your .htaccess file. If the mod rewrite was not configured correctly, I would expect to see 404 errors or the user being redirected incorrectly. As this does not appear to be the case; you need to widen your search to check that there are no security settings out of place.
I cannot see anything obvious in the httpd.conf file, but here are some of the steps you might want to take.
Remove the .htaccess file - if the website loads okay - then this file is causing an issue somehow; if it doesn't then there is something else wrong.
Check permissions on the web directory being used; there may be a permissions problem.
Check the httpd error log for suggestions.
Ask the question over at serverfault.com or poweruser stack exchange sites.
Ok! So I found the issue, The apache error_log file explained I needed to add this into my .htaccess file and the httpd.conf file -> "Options +FollowSymLinks +SymLinksIfOwnerMatch" and it worked! Now to create my VHost and I am good to go!!! Thanks!!!!!
Thanks
Ric

.htaccess redirect to subdirectory on Verio windows server

I've been trying to get a website up an running on my work's servers using Verio windows server 2003 gold and they've been quite useless when it comes to helping me out.
All I'm trying to do is set up a Wordpress installation, which they apparently require to be in a subdirectory. This is done and I have it in domain.com/wp ready to go. At this point they recommend that I use an .htaccess file to redirect to that url, so as they just sent me basically an "About .htaccess" link, that was of no help.
Did some searching, found a couple options like this one:
http://www.site5.com/blog/apache/htaccess/redirect-to-blog/20090427/
and nothing I use seems to work, not even as a straight redirect. Just gives me a "Directory Listing Denied - This Virtual Directory does not allow contents to be listed."
I do want to mention I did enable URL Rewrite for IIS via ISAPI_Rewrite 3 in the control panel.
As I mentioned Verio support is useless and I can't seem to find anything searching that seems to help my situation.
So pretty much what I need is domain.com/wp to rewrite as domain.com so all links centered around it do the same (ie domain.com/about domain.com/contact)
Any help? Thanks
If I understand you correct you need your Wordpress application to be available from the root of your web site instead of /wp/ folder? Then please try following .htaccess:
RewriteEngine on
RewriteRule ^(?!wp/)(.*) /wp/$1 [NC]
By the way I think you could try simply copying /wp/ folder content to the root folder.

.htaccess mod_rewrite trouble

So here's the thing .. I've been working on my webpage for some time now. I have a .htaccess in the root folder that contains only a few rows of rewriting rules and nothing else. It works fine on localhost but when I upload it to my host it just throws 404 error (my host is 000webhost.com). I tried eliminating every rule one by one to see where the error is, but nothing happened, I even deleted the contents of the .htaccess file and still it gives a 404 error. The only solution that worked (so far) was deleting the .htaccess file and only then the homepage appears. Any suggestions?
I just found out that my host doesn't support UTF8 encoding (i guess?) in .htaccess file so i changed the encoding to ANSI and it did the trick.
Have you tried the suggestion on your web host's site:
http://www.000webhost.com/faq.php?ID=16
they are suggesting you put this at the top of your .htaccess
RewriteBase /

.htaccess not working in subfolder

I have a couple of sites that are currently under development and I've put them under a specific subfolder for clients/co-workers to view - "http://dev.staffanestberg.com/site-name/". I've run into some problems getting the sites working with .htaccess. I can reach the index page by typing in the url for a site folder but neither img/style/js/etc linking or page rewriting works. Looking for a solution to this, Apache's "RewriteBase" seems to be the most likely one, however I can't get it to work.
Tried
RewriteBase http://dev.staffanestberg.com/site-name/
RewriteBase /site-name
What would be the correct method?
Thanks in advance,
-Staffan
mod_rewrite’s RewriteBase directive does only apply to rewrites using RewriteRule. And that does only apply for requests that are sent to the server.
But if the requested URIs are already wrong, RewriteBase doesn’t do any help. What you could try instead is the HTML element BASE that set’s the base URI for the current document.

Resources