Rewrite Rule causes "partially encrypted connection" in Firefox & Chrome - .htaccess

The question is based on the question htaccess rewriterule: redirect http to https or https to http (both ways) depending on URL typed. The great solution (thanks to Ulrich Palha) looks like this:
RewriteEngine on
RewriteBase /
#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]
#all pages that are supposed to be http redirected if https
RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
#all other pages are sent to https if not already so
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !1
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
Unfortunately, once I add this code to .htaccess file, everything works fine, but https started look broken, if contains links to css or pictures from the server, although the paths are relative.
The page below (https://www.example.com/?p=welcome) displayed with a broken SSL connection, FF browser says "Your connection to this site is only partrially encrypted"
<head>
<title>ddd</title>
</head>
<body>
Hello
<img src="/gfx/stars/help.gif" alt="" />
</body>
</html>
Why?
Once I remove <img src="/gfx/stars/help.gif" alt="" /> from the page , FF displays https fine, the URL bar is green then (SSL certificate is displayed). The same error appears, when I test it using Google Chrome.
So, what's wrong with <img src="/gfx/stars/help.gif" alt="" />? The path is relative.
The same happens if I add
<link rel="stylesheet" type="text/css" href="/css/mycss.css" />

Once the page has loaded and the browser starts requesting images and so on, over https. By my reading of your rewrite rules, because the path to the image has no query string IS_HTTP is set to 1. This then causes your rewrite rules to do a redirect to the http version of the image URL which is why the browser complains

One way to fix this is to avoid processing resources (since you don't have any special rules for them anyway) as follows
RewriteEngine on
RewriteBase /
#if its a resource (add others that are missing)
RewriteCond %{REQUEST_URI} \.(gif|css|png|js|jpe?g)$ [NC]
#do nothing
RewriteRule ^ - [L]
#rest of existing rules go here

Related

apache .htaccess url rewrite condition is causing 404 message

My .htaccess looks like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^VIN/(.*)/(.*)/ index.php?action=vin&view=$1&vin=$2
I can go to my url (foo.com) and it works just fine. However, if I go to foo.com/VIN/vinDetails/12345, I get a 404 error.
I'm new to the URL re-write, and have looked everywhere to try and figure this out, but I keep coming up short.
You can make trailing slash optional:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^VIN/([^/]+)/([^/]+)/?$ index.php?action=vin&view=$1&vin=$2 [L,QSA,NC]
css/js loading is due to your use of relative paths. Make sure path of these files start either with http:// or a slash /.
Otherwise You can add this just below <head> section of your page's HTML: <base href="/" /> so that every relative URL is resolved from that base URL and not from the current page's URL.

Rewrite resource URL to serve files from subdirectory

I have a static HTML site that uses resource tags (images, css, scripts) as follows:
<html>
<head><title>htaccess test page</title></head>
<body>
<img src="/img/img.jpg" alt="...">
</body>
</html>
When I load the html file in the browser, the request is made to:
http://localhost/img/img.jpg (which understandably returns a 404)
Where as I'd like the request to be made to:
http://localhost/site/img/img.jpg
The directory structure is as follows:
- www
- site
- .htaccess
- img
- img.jpg
I have been searching for a solution and have a vague idea that RewriteCond is the way to go, but I can't get this to work:
RewriteEngine On
# if requested URI is not a file
RewriteCond %{REQUEST_FILENAME} !-f
# serve the file from the img directory, yes, very limited,
# and requires me to add rules for scripts, css etc.
RewriteRule ^(.*)$ img/$1 [L]
Any help will be highly appreciated. SO returns quite a few solutions to this issue but none of them seem to work.
You solution is going to treat every file in this way and will re rout it. With this RewriteRule only images will be redirected to /site/{Path of the image defined in HTML}
RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ /site/$1 [R=301,L,NC]
You should put images like this <img src="img/img.jpg" alt="...">
The final version looks like this:
RewriteEngine On
# if requested URI is not a file
RewriteCond %{REQUEST_FILENAME} !-f
# image part
RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ /site/$1 [R=301,L,NC]

htaccess rewrite url with extra slash

I have this code for the htaccess file, and soon I'll have pages where I'd like to create a url like the bottom one
RewriteEngine On
RewriteRule ^Index/?$ index.php [NC]
RewriteRule ^Gallery/?$ gallery.php [NC]
RewriteRule ^Showreel/?$ showreel.php [NC]
RewriteRule ^Music/?$ music.php [NC]
RewriteRule ^Gallery/Render/?$ contact.php [NC]
If I go to the contact page, it'll display a page, but it's entirely white and only contains the text. I'm guessing it's to do with the images not being linked up properly with the new url, but how would I actually go about fixing this without having to manually edit the locations of each image?
You can try adding this in your page's header:
<base href="/" />
or change all of your links to absolute URLs. This issue is most likely that your links are all relative, and when you try to request a URL like:
http://yourdomain.com/Gallery/Render
The relative URI base becomes /Gallery/ instead of / (which is what it is if you access /contact.php directly). And when the browser tries to resolve all the relative links on the page, it uses the wrong URI base.

Canonical URL not redirecting

I want to get rid of the www. before my website name, so I tried setting a canonical URL tag in the files, but it didn't redirect me to http://blablabla.tk when I typed www.blablabla.tk
I tried this code: <link rel="canonical" href="http://blablabla.tk" />
Do I have to change something in the .htaccess file or something?
Yes, add this to the top of the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.blablabla.tk$ [NC]
RewriteRule ^(.*)$ http://blablabla.tk/$1 [L,R=301]
The "canonical" link that you have tells clients like search engine indexing bots which version of the page is the "preferred" version. It doesn't actually redirect anything.

RewriteRule giving 500 error when adding dynamic id at url end

I am getting a 500 Internal Server Error from my linked CSS files etc when I use this htaccess code.. Anyone know what might be the problem? I am not too fluent in htaccess yet.
Here is the code:
RewriteEngine On
RewriteBase /
RewriteRule ^(system|img|res) - [L]
RewriteRule ^picture/([^/]*)/?$ picture.php?id=$1 [L,QSA]
## The below code is something I found on the internet to remove the .php tag
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)index$ $1 [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
The URL is supposed to be: www.mysite.com/pictures/1 (id)
The id is always a number.
It does show me the page and I can echo the id, so that part is working, however it gives me a 500 error on linked files as mentioned above.
Not sure why it acts like that tho.. The CSS folder is in the same folder as the actual php file.
You've linked to it using a relative URI:
<link rel="stylesheet" type="text/css" media="all" href="./css/text.css" />
e.g. the ./css/text.css, and while the css file may be in the same directory as picture.php file (which I assume is what is generating the content) but the browser is what actually makes the request for the CSS, not the picture.php script. The browser requests this URL http://www.mysite.com/picture/1, and the server internally rewrite the /picture/1 to /picture.php?id=1, the browser has no clue that's happened. So it sees the base URI as /picture/. If the browser went directly to the php file: http://www.mysite.com/picture.php?id=1, the base URI would be / and the css would resolve just fine to /./css/text.css. But the /picture/1 request has a different base URI so the browser (with no clue that the base is different) blindly attempts to retrieve the css as /picture/./css/text.css, which fails because you have rules that mishandle that URI. Normally you'd just get a 404, but the rules you have after the picture rewrite mishandles the URI and returns a 500 server error.
You can either add in your header:
<base href="/">
in the content generated by picture.php, or make the URI's absolute:
<link rel="stylesheet" type="text/css" media="all" href="/css/text.css" />

Resources