htaccess - redirect everything to index.php except a folder and specific files - apache

I have been fighting with this for way too long. I have a my PHP site hosted with a .htaccess file for rewriting and redirecting. It has been working great so far. Now I simply want to add a subfolder, /pp that everything inside it does not get redirected or rewritten, basically it should not get touched by my .htaccess stuff.
Now when I go to mysite.com/pp/test.php (that file does exist), it redirects to mysite.com/index
Here is what I currently have:
RewriteEngine On
#remove www for all traffic
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#prevent redirect of submit
RewriteRule ^/?submit$ submit.php [L]
#prevent redirect of paypal
RewriteRule ^/?admin$ admin.php [L]
RewriteRule ^/?testPPButton$ testPPButton.php [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/pp/.* [NC] // this line is not working for some reason
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Options -Indexes
Updated .htaccess:
RewriteEngine On
# skip /pp/* from all rules below
RewriteRule ^pp(/.*)?$ - [L,NC]
#remove www for all traffic
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#prevent redirect of submit
RewriteRule ^/?submit$ submit.php [L]
#prevent redirect of paypal
RewriteRule ^/?admin$ admin.php [L]
RewriteRule ^/?testPPButton$ testPPButton.php [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Options -Indexes

Just below RewriteEngine On line add this to skip pp/ and everything under this folder:
RewriteEngine On
# skip /pp/* from all rules below
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+pp[/?\s] [NC]
RewriteRule ^ - [L,NC]
# rest of your rules go below this

Related

Redirect 301 appending old URL with New URL

I am having a problem while redirecting a page whose URL was changed but I want old URL to be redirected to new. Here is .htaccess file
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteEngine On
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
Using mod_rewrite in the last line I am not getting redirected to new URL but when I use mod_alias(Redirect) rule I get redirected to new URL but Old URL gets appended to in it in this format.
https://example.com/contact-us/?/contactus/
I have searched Stack Overflow for solution to this problem and tried many similar scenarios but that didn't work.
Have it like this the given order.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^contactus/$ /contact-us/ [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/system [NC]
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
RewriteEngine On is not required multiple times in same .htaccess.
Make sure to clear your browser cache before testing the change.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteEngine On
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
Your mod_rewrite external redirects (the last three rules) are in the wrong place. These need to be before the internal rewrite (front-controller). By placing them at the end of the file they are never going to get processed for any URL other than static resources, since all other URLs are routed to index.php, at which point processing stops.
when I use mod_alias(Redirect) rule I get redirected to new URL but Old URL gets appended
Different modules are processed independently regardless of the order of the directives in the config file. mod_alias is processed after mod_rewrite - but it is processed. However, the Redirect directive is prefix matching and everything after the match is appended onto the end of the target URL.
And there's no need to repeat the RewriteEngine directives
Have your directives like this:
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/system
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
And make sure to clear your browser cache before testing. Preferably test with 302 (temporary) redirect to avoid caching issues.

.htaccess: remove html extension, force www and https

I developed a simple and pure html site with this pages:
index.html
page1.html
page2.html
etc
And I would like to configure .htaccess to:
-Force https
-Force www
-Remove .html extension (/page1.html -> /page1)
-Redirect index.html -> /
-When someone types /page1.html to be redirected to /page1 (without html) or (if not possible) to 404 error page
How should I configure my .htaccess?
Thanks in advance
To remove .html and to force https://www , you can use the following rule :
RewriteEngine on
#force https+www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule (.*) https://www.%1/$1 [NE,L,R]
#Remove .html
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ /$1.html [L]
Clear your browser cache before testing these rules.
#OK we force https and www
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.exmple.com/$1 [L,R=301]
# remove .html from uri
RewriteRule ^([^\.]+)$ $1.html [NC,L]
# remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# redirect index.html -> /
RewriteCond %{REQUEST_URI} \.html$
Redirect /index.html /
All seems to work except when I enter example.com/page1.html. It doesn't remove html

Force user to use https:// + www

The target is the subdomain tc.gamingprouk.net and it is using valid SSL. I want .htaccess to add not only https:// or www., I want both of them! Like https://www.tc.gamingprouk.net.
This is what my .htaccess contains:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^tc.gamingprouk.net [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.tc.gamingprouk.net/$1 [R,L]
# Hiding file extensions
Options +MultiViews
RewriteEngine on
# For .php, .html & .shtml URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.shtml [NC,L]
Here is the problem:
Case 1:
User navigates to -> tc.gamingprouk.net
.htaccess rewrites to -> https://tc.gamingprouk.net
The problem: There is no www.!
Case 2:
User navigates to -> www.tc.gamingprouk.net
.htaccess does nothing!
The problem: It is without https!
Case 3:
User navigates to -> https://www.tc.gamingprouk.net
.htaccess rewrites to the same
The problem: Only in this case it is with both https:// and www.
You can use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE]
This checks to see if both www and HTTPs are on, if they're not, it will turn them on. Make sure you clear your browser cache before testing this.
Answered
I found out what you can Use. The following .htaccess code is part of google's:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
# Hiding file extensions
Options +MultiViews
RewriteEngine on
# For .php, .html & .shtml URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.shtml [NC,L]

htaccess to change url

I have the following code in my .htacess but it didn't work right. Is it because mod-rewrite is no "on", if so, how can i check?
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\$ $1.php [nc]
I wanted to rename my address, example:
http://www.abc.com -> http://www.abc.com
http://abc.com -> http://www.abc.com
http://www.abc.com/123.html -> http://www.abc.com/123
http://www.abc.com/12-12-12.html -> http://www.abc.com/12-12-12
http://subdomain.abc.com/123.html -> http://subdomain.abc.com/123
Basically removing the extension and ensuring that its www is intact.
Edited:
It was rewrote into
Options +FollowSymlinks
RewriteEngine on
RewriteCond % ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^(.*).php $1
but still not working
Step 1.
Change all of your links to have the .html removed from links. Make sure you have Multiviews turned off:
Options -Multiviews
Step 2.
You need rules to redirect the browser when there is a www missing from the hostname:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Step 3.
You need rules to redirect the browser when a URL is requested with .html or .php with the extensions removed:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.(php|html?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%2 [L,R=301]
Step 4.
You need rules to internally rewrite the URI if the request was actually for a php or html file:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ /$1.htm [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]
.htaccess
So all in all, you should have these rules in your htaccess (ditch any rules you may already have trying to solve this issue):
# Make sure Multiviews is off
Options -Multiviews
# turn on rewrite engine
RewriteEngine On
# Redirect requests that are missing www and not a subdomain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# Redirect if requests is for a .htm, .html, or .php
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.(php|html?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%2 [L,R=301]
# rewrite to the proper extension if such a file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ /$1.htm [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]
So altogether:
if browser URL address bar says: http://example.com/index.html it will be redirected to http://www.example.com/index
if browser URL address bar says: http://www.example.com/index it will be shown the content at http://www.example.com/index.html (or index.php if that exists). Address bar remains unchanged.
if browser URL address bar says: http://foo.example.com/images/image.png you will see the image at /images/image.png. Address bar remains unchanged.
if browser URL address bar says: http://foo.example.com/path/foo/bar.php it will be redirected to http://foo.example.com/path/foo/bar
if browser URL address bar says: http://foo.example.com/path/foo/bar it will be shown the content at http://foo.example.com/path/foo/bar.php. Address bar remains unchanged.
Remove .html:
RewriteRule ^(.*).html $1 [L,QSA]
Add www.:
RewriteCond % ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
On which hosting is located your website ? Are you sure mod-rewrite is enabled ? Did you contact your host to put it on, if not activated ?
Solution previously posted by androbin (https://stackoverflow.com/a/12553613/1713771) is correct.
The .htaccess file that I use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
</IfModule>
Which removes the .html file extension and forces www.

Simple URL Rewrite Problems

I'm having trouble re-writing a simple URL with .htaccess.
The URL I am trying to rewrite is:
http://www.domain.com/index.php?page=PAGE_NAME
I would like the PAGE_NAME to be directly after the domain, for example if PAGE_NAME is blog:
http://www.domain.com/blog
At the moment I have tried the following with no success:
RewriteRule ^/?([a-zA-Z0-9_-]+)?$ index.php?page=$1
All help is really appreciated.
Thanks in advance.
My current .htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
ErrorDocument 404 /search.php?page=notfound
# Add WWW to URL
RewriteCond %{HTTP_HOST} ^cristianrgreco\.com$ [NC]
RewriteRule ^(.*)$ http://www.cristianrgreco.com/$1 [L,R=301]
# Remove trailing slashes from end of URL
RewriteCond %{HTTP_HOST} !^\.cristianrgreco\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
# Rewrite main page URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC]
RewriteRule ^ %1? [L,R=301]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC]
# Rewrite download URLs
RewriteRule ^download/([a-zA-Z0-9._-]+)/?$ download.php?file=$1
# Rewrite page navigation links
RewriteRule ^(.+?)/page-([a-zA-Z0-9_-]+)?$ $1.php?currentpage=$2
# Rewrite article URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ articles.php?article=$2
# Remove file extension from PHP files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA]
</IfModule>
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#add these next 2 lines if you need to redirect http://www.domain.com/index.php?page=PAGE_NAME to http://www.domain.com/PAGE_NAME
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC]
RewriteRule ^ %1? [L,R=301]
#send request to index.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC]