Yii switching to HTTPS - .htaccess

I know, that switching to HTTPS has nothing to do with Yii, and it is mainly adding
rewrite rules in .htaccess.
Now I am facing some problems. After I changed my .htaccess file
from
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# manual change the url base
RewriteBase /
# otherwise forward it to index.php
RewriteRule . index.php
to
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://my.domain.de/$1 [R,L]
# manual change the url base
RewriteBase /
# otherwise forward it to index.php
RewriteRule . index.php
The first login page is automatically redirected from http to https as I wanted, but there are no css and images loaded. I have noticed that all the paths to these files are relative, so https://my.domain.de/css/print.css is not found, but http://my.domain.de/css/print.css is accessible. And if I go through the links in web-site css and images are everywhere missed.
Any suggestions?

Rewrite conditions only affect the immediately following rule. The two !-f and !-d conditions need to be applied to the routing rule: RewriteRule . index.php. Otherwise, everything blindly gets routed to index.php and request for files that actually exist won't get fulfilled.
Additionally, the redirect needs to be before the routing rule, so you need to move your conditions to be right above your routing rule:
RewriteEngine on
# manual change the url base
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://my.domain.de/$1 [R,L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

Related

.htaccess: redirect if file doesn't exists

It is quite popular question, but there is some distinction in my case that makes all more difficult.
Apache2 web server, Yii2 framework with prettyUrl turned on.
Here is initial .htaccess file, provided by Yii2:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
There is /docs web directory. And I would like to get files from it if they are exist and redirect to /docs-generator/create?file=<file_name> if the file doesn't exists.
So, I have added a rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/docs
RewriteRule ^docs/([\w-_.]+) http://kz_proj.test/docs-generator/create?file=$1 [L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
It's works but quite strange... First of all, I can't remove my test hostname from http://kz_proj.test/docs-generator/create?file=$1. It doesn't work without full name. The second one is about ^/docs and ^docs/([\w-_.]+) without the leading character. Why first part of these rules can't be the same in my case?
I guess something wrong with the rules...
You can use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^docs/([\w-_.]+) /docs-generator/create?file=$1 [L,R=301]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule ^ index.php
It works with the domain name because it is necessarily a redirect in this case, but not without. I added [R=301] to force the redirection.
No need to add RewriteCond %{REQUEST_URI} ^/docs
because the test is the same with RewriteRule ^docs
And why first part of these rules can't be the same in your case ?
REQUEST_URI always start with a /
and RewriteRule test path never start with a / in .htaccess (why????)

.htaccess code to affect main domain only

I have two domains. 1) main 2) subdomain. This code in the .htaccess file is causing me problems.
# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !=/favicon.ico
# RewriteRule ^ index.php [L]
As is, everything looks perfect on both sites and runs smoothly for the end user. However, this code means we can't make any admin changes on the main domain (page edits, creating content, etc).
If I "uncomment" the bottom 4 lines, I can make changes to the main domain, but the subdomain no longer points to the appropriate theme files.
Is it possible to "uncomment" the bottom 4 lines and have it only affect the main domain?
You can use this .htaccess:
RewriteCond %{HTTP_HOST} !^subdomain\.example.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
To exclude the subdomain.

Redirecting all requests from a subdirectory to a specific file

I wanted to redirect all requests from a specific subdirectory (/portfolio/*) to a specific file in the root of my website (portfolio.php). This way I want to be able to create project-specific pages within the same file as where the overview is being created.
I tried the following, but it didn't work since it stopped images from loading (images were located in the /images/portfolio/* directory), and it also disables my custom ErrorDocument...
RewriteRule /portfolio/(.*) /$1 [PT]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /portfolio.php [L]
Anyone has an idea on how to fix this?
Try getting rid of the first rule and adding the pattern to your last rule:
RewriteRule ^portfolio.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^portfolio/(.+)$ /portfolio.php [L]
And for good measure, make sure multiviews is turned off:
Options -Multiviews

Redirect file type specific requests

In my app, I have the srcs of many images like this:
path/other-path/my-image.jpg
This can get long and can expose some of my file structure to the outside world.
What can I do in .htaccess to redirect a given file extension to a directory?
i.e. my image src is simply "my-image.jpg", .htaccess sees that is a .jpg file and redirects to the folder "www.site.com/my-jpgs/" were "my-image.jpg" resides, thus loading the image.
Here is what I have at the moment:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/importers
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$
RewriteRule ^(.*)$ public_html/index.php
RewriteBase /
# Ignore rule if the real path exists, use that instead.
RewriteCond %{REQUEST_FILENAME} !-f
# Only use the filename and extension.
RewriteRule (.[^/]*)\.(gif|jpg|png) public_html/images$1.$2 [R,L]
I've sort of got it working, except the browser makes two requests that look like this:
Request URL:http://www.view.com/activity/enquiry/dropdown-btn.gif
Request URL:http://www.view.com/public_html/images/dropdown-btn.gif
The second being correct, how do I correct this so it doesn't make the first incorrect request?
Ordering of rewrite rules is very important in .htaccess. Your problem is incorrect ordering of your rule. You need to move last rule to the top just belowRewriteEngine Online to have external redirect before sending everything off toindex.php`:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteBase /
# Ignore rule if the real path exists, use that instead.
RewriteCond %{REQUEST_FILENAME} !-f
# Only use the filename and extension.
RewriteRule ^(?:[^/]+/)*([^.]+\.(?:gif|jpe?g|png))$ images/$1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/importers
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$
RewriteRule ^ index.php [L]

Configure htaccess to load static pages

Im working on a server with the following htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php
RewriteRule ^[^/]*\.html$ index.php
RewriteRule ^/(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)/ - [L]
RewriteRule ^/(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)/.*$ - [L]
Now they asked me to prepare a static page inside their server, lets call it http://www.myserver.com/mystaticpage.html
The problem is that when i try to access that url, it redirects to index.php. How could I alter the htacces file to address this problem without messing anything with the installed CMS?
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond ${REQUEST_URI} !^/(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)
RewriteRule .* index.php
The rules you used before were redundant: if .* is rewritten to index.php then why also rewrite ^[^/]*\.html$ index.php to it? it already matched previous rule...
They also overlapped - since the three RewriteCond conditions were only applied on the first rule. So the second rule was also applied to static files on disk.
Also, the two rules that were listed last had no effect whatsoever. Either you needed to list them first, or not at all. I converted them to an additional RewriteCond since they were only attempted to avoid rewrite on certain uris

Resources