htaccess hide extension doesn't allow ExpressionEngine logins - .htaccess

So pretty much said it but, using this in my .htaccess
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## removes index.php
# Add/Hide index.php from everything except admin
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond $1 !admin$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
## hide .php extension
# To externally redirect foo.php to foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Will allow me to use /admin as a url, but will not allow me to log in,
If I remove this line RewriteCond $1 !admin$ [NC] it prepends index.php onto
/admin making it index.php/admin and I can't see the page

What are you trying to achieve? Just removing index.php? If so this .htaccess works:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Then you'll have to access /admin.php of course, or you can put it in a directory named /admin if you wish.

Related

How externally redirect url using htaccess, when url contain space, number or dash

I am new in htacces.
I want to create htaccess like stackoverflow.
Check any url of stackoverflow like "hide file extension in url by htaccess". If you put .html/.php/.asp/.abc/.xyz anything it will redirect to "hide file extension in url by htaccess" only even you put / at last it has no effect
Means I want to say url filename contain any keyboard character and it will redirect externally.
below are my current htaccess
RewriteEngine on
# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
You need to use appropriate ``RewriteBase`:
RewriteEngine on
RewriteBase /folder1/folder2/
# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^GET\s(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
My final htaccess file with the help of great #anubhava
RewriteEngine on
RewriteBase /folder1/folder2/
# To externally redirect /folder1/folder2/file.html to /folder1/folder2/file.html or any extension like .php/.asp/.abcd etc
RewriteCond %{THE_REQUEST} ^GET\s(.+?)\.[a-z0-9]{2,}[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

htaccess - HTTP to HTTPS and .HTML to /

I need help with an .htaccess that:
forces http:// to https://
AND
forces .html to /
What I have so far:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Options -MultiViews
RewriteEngine On
#1 This line checks if the https is off
RewriteCond %{HTTPS} ^off$
#then, redirect to https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [NC,L,R]
#2 this line checks if the request is /file.html
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
#then redirect /file.html to /file
RewriteRule ^ /%1 [NC,L,R]
#3 if the request is not for dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is an existing filename
RewriteCond %{REQUEST_FILENAME}.html -f
#then rewrite /file to /file.html
RewriteRule ^([^/]+)/?$ $1.html [NC,L]
In the example above, the first condition is met when the original scheme is HTTP, and then the rule is processed. HTTP goes to HTTPS. The first Round of rewrite processing ends here.
In the second round, mod_rewrite accepts the URI /file.html and the rule redirects it to /file, since the /file does not exist in directory, so we need to rewrite it to the original file #3...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I've used both of them before, but I haven't used them together. Hope this helps.
(EDIT)
To actually FORCE extensions to look like directories:
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=302,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
http://www.inmotionhosting.com/support/website/ssl/how-to-force-https-using-the-htaccess-file
First of all, make sure mod-rewrite is enabled.
Then, put this code in your htaccess (which should be in document root folder)
Options -MultiViews
RewriteEngine On
RewriteBase /
# Redirect http urls to https equivalent
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://{HTTP_HOST}/$1 [R=301,L]
# Redirect existing /path/file.html to /path/file/
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+?)\.html\s [NC]
RewriteRule ^ %1/ [R=301,L]
# Internally rewrite back /path/file/ to /path/file.html (if existing)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+)/$ $1.html [L]
Warning:
Make sure to serve the same document root as http for https (apache ssl block configuration)
Be aware that this creates virtual directories (by adding a trailing
slash) which could mess up your html resources (if you're using
relative paths instead of absolute paths). If so, use absolute paths
instead

.htaccess redirect but keep url

I have the following code and I am trying to do something like that. Let's say I have an address - mydomain.com/server2/ when I type anything after slash for example mydomain.com/server2/whatever I want to load server2.php but address has to be the same
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# remove index.php
RewriteRule ^index\.php/?$ / [L,R=301,NC]
# Hide File Extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# Add 301 redirects to new extensionless file
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.mydomain.com/$1 [R=301,L]
# Add the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://www.mydomain.com/$1/ [R=301,L]
</IfModule>
also I don't know if above code is 100% correct. Thanks for any help.
There are problems in your code and in certain URLs it will give you infinite looping. Replace your code with this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,L,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
# Add the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
</IfModule>

URL rewrite working for .html but not .php

I have a URL rewrite that strips away the file extension. It's working with .html pages but it gives me a "404 Page Not Found" error with .php files.
Here is my full .htaccess file
# The following will allow you to use URLs such as the following:
#
# example.com/anything
# example.com/anything/
#
# Which will actually serve files such as the following:
#
# example.com/anything.html
# example.com/anything.php
#
# But *only if they exist*, otherwise it will report the usual 404 error.
Options +FollowSymLinks
RewriteEngine On
rewritecond %{HTTP_HOST} ^inaflashgraphics.com$
rewriterule ^ "http\:\/\/www\.inaflashgraphics\.com\/" [R=301,L] #4e2f2fa615667
# Remove trailing slashes.
# e.g. example.com/foo/ will redirect to example.com/foo
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
What am i doing wrong?
i figured it out, this code works for me.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
rewritecond %{HTTP_HOST} ^inaflashgraphics.com$
rewriterule ^ "http\:\/\/www\.inaflashgraphics\.com\/" [R=301,L]
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
You can try:
RewriteCond %{HTTP_HOST} ^www\.inaflashgraphics\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

Remove the file extension .php using htaccess file

I want to remove the .php from the url through htaccess file. for example home.php to home
I'm using the following rewrite rule in htaccess file.
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
I also want to assign the login as index.
How can I change it.
This is the code you can use to hide .php extension:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L,NC]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp
To add a trailing slash at the end of the URL
(for example: http://www.example.com/about-us/ to go to http://www.example.com/about-us.html)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Resources