Apache Mod Rewrite for CodeIgniter AND HTML5 mode - html

I have Codeigniter running on Apache and have Apache Mod Rewrite that works well.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I've now changed the front-end to be HTML5 and my app uses emulated URLs that suppose to be handled by the same 1-page application.
So I need something like the following URL rewrite:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
So, I'd like to combine those together.
First I'd like to see if the first part of URL path is a CI controller (a file in ./application/controllers) and if yes then rewrite it to index.php.
If not, I'd like to rewrite it to index.html.
I'm trying to build something like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# -- RewriteCond ^/([^/]+)/ (application/controllers/$1.php) is a file
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^ index.html [L]
I understand that my other option it to manually specify all possible routes and controllers but would be nice to do it automatically...
UPDATE:
The proposed solution below helped me to solve the problem.
This is what worked for me eventually:
RewriteCond C:/projects/someproject/WS/application/controllers/$1.php -f [OR]
RewriteCond C:/projects/someproject/WS/application/controllers/$1 -d
RewriteRule ^(.+)$ index.php/$1 [L]
RewriteCond $1 ^(.*)/$
RewriteCond C:/projects/someproject/WS/application/controllers/%1 -d
RewriteRule ^(.+)$ index.php/$1 [L]
RewriteCond $1 ^(.*)/[^/]*/?$
RewriteCond C:/projects/someproject/WS/application/controllers/%1.php -f
RewriteRule ^(.+)$ index.php/$1 [L]
# else, rewrite the request to /index.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.html [L]
I've realised I had to check for more types of URLs and also I found I couldn't use %{DOCUMENT_ROOT} because I'm using VirtualDocumentRoot settings and DOCUMENT_ROOT isn't working

Try :
# If /document_root/application/controllers/request.php is a file
RewriteCond %{DOCUMENT_ROOT}/application/controllers/$1.php -f
# Rewrite the request to /index.php
RewriteRule ^(.+)$ /index.php/$1 [NC,L]
# else, rewrite the request to /index.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.html

Related

How do I remove the .html from the end of URL while using SiteGround

I had originally built my website in React before learning that my client is using SiteGround so I had to change everything over to a basic HTML format. I have my navbar using href = "fileName.html" to navigate the page. This leads the URL to become www."siteName".com/"fileName.html" once navigating off the homepage.
This is what I have so far in my .htaccess to remove the .html from the end:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
This does not seem to be working. Should I change my .htaccess file? Should I change the way my navbar navigates the pages?
With your shown samples/attempts, please try following .htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /
###Rules for handling .html to without html uri in browser.
RewriteCond %{REQUEST_URI} \s/([^.]*)\.html\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.*)/?$ $1.html [L]
##Rule from OP's already existing .htaccess file.
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>

Remove public from url laravel 5.4

I am working on laravel 5.4 project and want to remove public from url.Right now my url is localhost/project_name/public/login.
Please suggest me way remove public from url.
Step 1: Rename File
In first step it is very easy and you need to just rename file name. you have to rename server.php to index.php at your laravel root directory.
server.php
INTO
index.php
Step 2: Update .htaccess
first of all you have to copy .htaccess file and put it laravel root folder. You just copy .htaccess file from public folder and then update bellow code:
.htaccess
Options -MultiViews -Indexes
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_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
Documentation : https://hdtuto.com/article/laravel-remove-public-from-url-using-htaccess
Create a new .htaccess file in your root and paste following code.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
ONE OF THE BEST SOLUTION

remove www and html while forcing https

How would i remove www from my url and force https and remove .html all at the same time?
RewriteEngine on
# force ssl
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
# Remove .html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
Try adding the following to your .htacces instead of your code above.
RewriteEngine on
#Force HTTPS.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Start rewrite.
RewriteBase /
# Load page without extension.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# 301 While Rewrite.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
# Handle Trailing Slashes.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]
# Rewrite rule to add .html extension back internally.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+[^/])$ $1.html
This will result in http://www.example.com/test.html to be handled by the browser as https://example.com/test while still loading the test.html file.
Put the following code at .htaccess in main directory
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://yoursite.com/$1 [L,R]
#the code above will redirect the entire site into https without www unless the request come with https://www .
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^ https://yoursite.com/$1 [L,R]
#The code above will catch any request with www if passed first code.
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.html[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
#the code above will remove any .HTML.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
# you can use the two lines if file without extension not work and you want to map them to .html files and if you want to go the same file .html without extension keep it as it is but , If you want to change it to same name .php file , replace $.html in the last line to $.php .

htacess: triple rewrite HTTPS + no-WWW + no-.html

I am trying to write a multi rewrite function in htacess.
change http into httpS
remove www
remove .html
It should handle all the cases:
http://www.example.com/about.html
should be rewritten as
http S://example.com/about
But of course if there is only one item to rewrite it should work too:
http://example.com/about
should be rewritten as
http S://example.com/about
Here is my code (https and no-WWW work but the no-.html which is commented does not work).
Please note that when rewriting you need to use the previously rewrited URL and not what the user typed into his address bar otherwise the rule will overwrite the change that another rule did.
RewriteEngine on
#strip the www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
#remove .html if any
#RewriteCond %{REQUEST_FILENAME}.html -f
#RewriteRule ^(([^/]*/)*[^/.]+)$ /$1.html [L]
#switch to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$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
# Disable the server signature
ServerSignature Off
You can use:
RewriteEngine on
#strip the www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
#switch to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
#remove .html if any
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# 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
# Disable the server signature
ServerSignature Off

Exclude all Files from Rewrite (NGINX to APACHE Migration)

My URL structure is like
http://www.example.com/folder/index.php?dir=dir1
To be able to access it from
http://www.example.com/folder/dir1
and at the same time redirect the 1st URL to 2nd one, my htaccess (in 'folder') is
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301,NE]
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]
The Redirect and Rewrite were working perfect until recently I switched from NGINX back to APACHE.The trouble is that it is now also rewriting file extensions which I don't want!How can I only apply it only to directories and exclude ALL files from it?
Figured it out! :)
Actually had to add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
before the first rewrite!
So the htaccess looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]