htaccess file which redirects my advanced Yii2 app to frontend index.php file for that there is already an .htaccess file. which has following lines..
This is Right Now my .HTACCESS at root directory
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
</IfModule>
# Deny accessing below extensions
<Files ~ "(.json|.lock|.git)">
Order allow,deny
Deny from all
</Files>
# Deny accessing dot files
RewriteRule (^\.|/\.) - [F]
now i searched internet and i found this if i want to redirect to https then i have to add this.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
so i added like this....
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
</IfModule>
# Deny accessing below extensions
<Files ~ "(.json|.lock|.git)">
Order allow,deny
Deny from all
</Files>
# Deny accessing dot files
RewriteRule (^\.|/\.) - [F]
then it loads website withouat any js and css... and also it is in http. But content it requires should be in https.
I do understand it is because of my another rewrite statement. But i am not so expertise in it. Please mae some suggetion.
Here this thing if it is alone it works perfect without any problem.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
It will send my http to https. So it works perfact.
Or if this is alone
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
This rule tells that execute index.php file from the [.htaccess Directory/frontend/web/] directory . Means execute frontend/web/index.php . Here it is very important to do this as this is as per the framework structure.
So i have to combine these two rules together and build a .htaccess which will work for both scenario.
CONCLUSION
So my .HTACESS should tell the server we have to run [.HTACESS DIR/frontend/web/index.php] in https.
UPDATE TO QUESTION
this is the .htaccess under frontend/web/ folder where my index.php is
And this is at root/frontend/web folder where index.php exists.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Do i have to add https thing here ?
The easiest way might be setting the on beforeRequest event handler in /config/web.php like so:
...
'bootstrap' => ['log'],
'on beforeRequest' => function ($event) {
if(!Yii::$app->request->isSecureConnection){
$url = Yii::$app->request->getAbsoluteUrl();
$url = str_replace('http:', 'https:', $url);
Yii::$app->getResponse()->redirect($url);
Yii::$app->end();
}
},
...
You root/frontend/web/.htaccess should be like this:
RewriteEngine on
RewriteBase /frontend/web/
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Just remember that a sub-directory .htaccess always takes precedence over parent directory .htaccess.
In root .htaccess when i put this. It also works for me.
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
Another approach would be to change the virtual host on port 80 (HTTP):
<VirtualHost *:80>
ServerName mywebsite.com
Redirect permanent / https://mywebsite.com
</VirtualHost>
and then have a separate virtual host for port 443 (HTTPS).
Assuming a Debian(-fork) Linux server you'd have to put the above in /etc/apache2/sites-available/myname.conf.
After you've done that use sudo a2ensite myname.conf (if this fails, you can create the symlink in /etc/apache2/sites-enabled/ yourself, but try not to). Now restart apache with 'sudo service apache2 restart'.
I've used this code below.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Related
I have a laravel application installed on a shared hosting package and I'm using https://github.com/JosephSilber/page-cache to cache pages.
The cached pages are stored in the following server folder /home/username/public_html/app_name/public/page-cache/article
The following code is in the .htaccess found in public folder
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Serve Cached Page If Available...
RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{DOCUMENT_ROOT}/public/page-cache/pc__index__pc.html -f
RewriteRule .? public/page-cache/pc__index__pc.html [L]
RewriteCond %{DOCUMENT_ROOT}/public/page-cache%{REQUEST_URI}.html -f
RewriteRule . public/page-cache%{REQUEST_URI}.html [L]
RewriteCond %{DOCUMENT_ROOT}/public/page-cache%{REQUEST_URI}.json -f
RewriteRule . public/page-cache%{REQUEST_URI}.json [L]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I have tried to debug the values of DOCUMENT_ROOT AND REQUEST_URI and I'm getting the following:
%{DOCUMENT_ROOT} = /home/username/public_html/app_name and %{REQUEST_URI} = /article/race-policing-and-the-universal-yearning-for-safety
With the above code in .htaccess i'm not managing to direct it to the cache pages but its going directly to the application even though the file exists.
e.g. /home/username/public_html/app_name/public/page-cache/article/race-policing-and-the-universal-yearning-for-safety.html
There is also the following .htaccess file in the app_name folder.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
#disable directory browsing
Options -Indexes
#PROTECT ENV FILE
<Files .env>
order allow,deny
Deny from all
</Files>
#PROTECT ENV FILE
<Files .htaccess>
order allow,deny
Deny from all
</Files>
I would appreciate if someone can help me identify the reason why the .htaccess is not loading the cache page when it exists. Stay safe guys :).
How can I remove /public/ from url and auto force http to https?
my root .htaccess (I created it)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{HTTPS} !on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
public/.htaccess (by default laravel)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
But it not works - no auto http to https redirect.
So I neet to combine removing public folder from url and forcing http to https using .htaccess
The problem you're having is that you've set your document root to be the root of the project. In your apache virtual host config you need to set the document root to be the public directory.
For example:
DocumentRoot /var/www/yourproject/public
That way, public doesn't appear in the URL.
If you're using shared hosting you'll want to move the root of your project to above public_html/, delete public_html, then rename your public directory to public_html. You can then go and modify the bootstrap files to use this path. You could also symlink it.
Add this two line into your Public Folder htaccess file
RewriteCond %{HTTPS} !=on
RewriteRule .* https://yourdomain.com%{REQUEST_URI} [R,L]
Full Code of your public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://yourdomain.com%{REQUEST_URI} [R,L]
</IfModule>
I think you need to enable rewrite_url with
$ sudo a2enmod rewrite
and
restart your apache server
$ sudo systemctl restart apache2
Your site-enabled/000-default.conf
DocumentRoot /var/www/html/yourapp/public
May be you can use
RewriteBase "yourapp/public/"
or refer this answer.
Rename server.php in your Laravel root folder to index.php
Copy the .htaccess file from /public directory to your Laravel root folder.
Put this code in .htaccess file of root directory of domain for removing 'public :
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
And for redirecting to https from http add following lines at the top in .htaccess file located in public directory of laravel project.
RewriteCond %{HTTP_HOST} your_domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.your_domain.com/$1 [R,L]
My htaccess
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|uploads|images|ws|vendor|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
I have also hidden my index.php
Fails to load: -
https://www.abc.in/abc-xyz-fdg
Works: -
https://www.abc.in/index.php/abc-xyz-fdg
Try this
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
You need to make changes in two files.
config.php file $config['secure_base_url']=" https://www.abc.in/";
And open the url_helper.php file (system/helpers/url_helper.php) and alter the code in https://sajjadhossain.com/2008/10/27/ssl-https-urls-and-codeigniter/
Now, add the following code in Config.php file (system/Core/Config.php) as in https://sajjadhossain.com/2008/10/27/ssl-https-urls-and-codeigniter/
reference
This worked for me.
Step 1
SSL config (which runs with https)
<Directory "/var/www/html">
AllowOverride All
</Directory>
Step 2
The .htaccess file should be like this...
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
Reference link :
HTTPS and Remove index.php on CodeIgniter
Thank you guys who tried to him me
Happy Coding!!
I am using an AWS EC2 instance to host a website. I have an installation under /var/www/html/xyz/ dir & thus if I have to access website homepage, I have to specify link as www.example.come/xyz. I want that www.example.com should be redirected to www.example.com/xyz
Any idea how can I redirect www.example.com to www.example.com/xyz automatically (httpd config or anything similar)?
I checked online but most of the posts are related to subdomain redirects e.g. redirect admin.example.com to www.example.com which is not my issue.
Thanks!
my .htaccess file is:
<IfModule mod_rewrite.c>
RewriteEngine on
# Some hostings require RewriteBase to be uncommented
# Example:
# Your url is http://www.yourcompany.com/xyz
# So "RewriteBase" should be:
# RewriteBase /xyz
Options -MultiViews
RewriteBase /xyz
RewriteCond %{REQUEST_URI} ^api/(.*)$ [or]
RewriteCond %{REQUEST_URI} .*/api/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*api/(.*)$ api.php?_d=$1 [L,QSA]
RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml)$ [or]
RewriteCond %{REQUEST_URI} store_closed.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/(.*)$ $2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
</IfModule>
Another option is to change the document root of your site. If you are running Ubuntu, you should have a file in /etc/apache2/sites-available/YOUR_SITE.conf (maybe default.conf). Just edit it and change the DocumentRoot to point to your subfolder.
Create new .htaccess file with following code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /xyz/ [R=301]
you should have mod_rewrite enabled.
Use this code in your htaccess file, this will redirect the user to the subfolder without changing th URL www.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule !^subdir/ /subdir%{REQUEST_URI} [L,NC]
Another method with URL www.domain.com/subdir
RewriteEngine On
RewriteRule ^$ /subdir[L]
You can use the following code in Root/.htaccess:
RedirectMatch ^/$ /xyz
This will redirect
http://example.com
to
http://example.com/xyz
And you want to Rewrite http://example.com/foo to http://example.com/xyz/foo , you can use mod_rewrite :
RewriteEngine on
RewriteRule ^/?((?!xyz).*)$ /xyz/$1 [L]
Hope this will help !
RewriteEngine on
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /subfolder/$1 [L]
or this
Redirect 301 /path/to-old-url http://www.cyourdomain.com/path/to-Try this new-url
I want to access http://newdomain.co/ and view all the content from http://olddomain.com/shop/, but with the url bar showing the new domain and keeping all the old structure.
The e-commerce platform is OpenCart. In the .htaccess located under /shop/ folder I currently have this code:
Options +FollowSymlinks
Options -Indexes
<FilesMatch "\.(tpl|ini|log)">
Order deny,allow
Deny from all
</FilesMatch>
RewriteEngine On
RewriteBase /shop/
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule sitemap.xml /index.php?route=feed/google_sitemap
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
I'm not used to htaccess codes, so explain well, please :)
You need to use mod_proxy to do this. And you can't use mod_proxy unless it's loaded in your apache server. If it's loaded, you can add this to the htaccess file that's in the document root of the newdomain.co host:
RewriteEngine On
RewriteCond %{HTTP_HOST} newdomain\.co$ [NC]
RewriteRule ^(.*)$ http://olddomain.com/shop/$1 [L,P]
If nothing happens, or you're getting redirected to the old domain, then you don't have mod_proxy loaded.
If both domains are using the same document root, then you don't need to use mod_proxy, you just need this in the document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} newdomain\.co$ [NC]
RewriteRule ^(.*)$ /shop/$1 [L]