.htaccess not redirecting to .php files - php

I need to redirect the following pages to their .php versions.
http://www.kgolf360.com/about
http://www.kgolf360.com/news
http://www.kgolf360.com/junior-performance-programs
http://www.kgolf360.com/before-after
http://www.kgolf360.com/book-lesson
http://www.kgolf360.com/family-performance-programs
http://www.kgolf360.com/technology
http://www.kgolf360.com/adult-performance-programs
For example, I need to redirect
http://www.kgolf360.com/about
to
http://www.kgolf360.com/about.php
etc.
What I did is modified my .htaccess file by adding the following:
Redirect 301 /about http://www.kgolf360.com/about.php
but it gives me a 404 error. You can see it if you go to http://www.kgolf360.com/about
Is it possible that I need to configure something else to make it work? If so, what exactly do I need to do to redirect the URLs properly?
Here's the content of my .htaccess file:
rewriteengine on
rewritecond %{HTTP_HOST} ^www.kgolf360.com$ [OR]
rewritecond %{HTTP_HOST} ^kgolf360.com$
rewriterule ^about$ "http\:\/\/kgolf360\.com\/about\.php" [R=301,L] #5682a68c22ecf
rewritecond %{HTTP_HOST} ^kgolf360.com$
rewriterule ^(.*)$ "http\:\/\/www\.kgolf360\.com\/$1" [R=301,L] #567eeb84dcae2
# 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
# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
# 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}.html -f
rewriterule ^(.+)$ $1.html [L,QSA]
rewritecond %{REQUEST_FILENAME} !-f
rewritecond %{REQUEST_FILENAME} !-d
rewritecond %{REQUEST_FILENAME}.php -f
rewriterule ^(.+)$ $1.php [L,QSA]
#Redirect all non-php URLs to their php versions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Do you guys see any issues with the content I have in the fle now?

Since you are using Godaddy, you need to use Options +MultiViews. So try this code:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Related

htaccess rewrite giving off .php at the end of a paremeter

I have created my own .htaccess file which purpose is to change the username URL of my site (along with remove the .php extension from all urls).
The intent is for the URL to transform from:
www.example.one/profile.php?id=username
to
www.example.one/username
I know this is a common problem with "username" htaccess rewrites, however, none of the answers I have found have helped me solve my problem.
This is my full .htaccess:
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
# only allow rewriting to paths that dont exist
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# redirect example.one to www.*
RewriteCond %{HTTP_HOST} ^example\.one$ [NC]
RewriteRule ^(.*)$ http://www.example.one [R=301,L]
# no php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# example.one/username
RewriteRule ^/([a-zA-Z_\-0-9]+)*$ ./profile.php?id=$1
Cheers.
You may use these rules in your site root .htaccess:
Options +FollowSymLinks -Indexes
RewriteEngine On
# redirect example.one to www.*
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /profile\.php\?id=([\w-]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# no php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# example.one/username
# only allow rewriting to paths that dont exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ profile.php?id=$1 [L,QSA]

Using htaccess to redirect user profiles

I am creating a social networking site. Using .htaccess I have successfully removed .php extensions from urls but I am not able to redirect domain.com/u.php?username=[username] to domain.com/u/[username]. Following is my .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
DirectoryIndex home.php
I have seen many examples and codes in stackoverflow but all of them redirect from domain.com/user.php?username=[username] to domain.com/[username]. But this would affect my website since I have removed .php extensions and I might end up with name clash. Thanks in advance.
You can put this code in your root htaccess
DirectoryIndex home.php
Options -MultiViews
RewriteEngine On
RewriteBase /
# redirect /u.php?username=XXX to /u/XXX
RewriteCond %{THE_REQUEST} \s/u\.php\?username=([^&\s]+)\s [NC]
RewriteRule ^ u/%1? [R=301,L]
# hide php extension (if file exists)
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+)\.php(?:\?|\s) [NC]
RewriteRule ^ %1? [R=301,L]
# internally rewrite /u/XXX to /u.php?username=XXX
RewriteRule ^u/([^/]+)$ u.php?username=$1 [L]
# internally rewrite (if it exists) extensionless to php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)$ $1.php [L]

mod_rewrite: dynamic redirect and directory blocking

I am working on an .htaccess-file lying in mysite.com/dir/.htaccess that redirects mysite.com/dir/page to mysite.com/dir/page.php if possible and redirect to mysite.com/dir/ if not possible. So any "wrong" request will be redirected to the main page. The code im using is:
RewriteEngine on
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [R]
This all works well. (By the way: i want to determine the base-dir of the file, because i maybe want to change the directory name later, but not change the file). What i now want is to block some directories for the user, because they contain php-libraries that should not be accessible. how can i e.g. block the directories mysite.com/dir/lib/* and mysite.com/dir/lib2/* and redirect them to mysite.com/dir/ again like i did before? I also want to use code not like
RewriteRule ^lib/(.)* %{ENV:DIR_BASE}
that would redirect mysite.com/dir/lib/../page to the main page instead of mysite.com/dir/page where it should belong. i tried very much with %{REQUEST_FILENAME} and %{REQUEST_URI}, but i am only a beginner when it comes to mod_rewrite. do you know a solution?
This should work:
RewriteEngine on
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
RewriteRule ^lib2?(/|$) %{ENV:DIR_BASE} [L,NC,R]
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.*)$ $1.php [L]
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [L,R]

How to redirect requests with .php (extension)

I've found a partial answer here.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php - [F,L]
[works] www.example.com/file.php to redirect to 404
[works] www.example.com/file to serve under /file.php (without showing on address bar)
[works] www.example.com/folder/ to NOT redirect to 404
[does not work] www.example.com/folder/index.php to redirect to 404
I'm aware that www.example.com/folder/ is served as www.example.com/folder/index.php (without showing on address bar) because of DirectoryIndex
I've tried playing around and disabling DirectoryIndex but I just couldn't figure it out.
What I have so far:
# if file has a .php extension redirect to 404 page #
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteCond %{THE_REQUEST} !^[A-Z]{3,}\s/+index\.php(/[^\s\?]+)? [NC]
RewriteCond %{REQUEST_URI} !(.*)/index\.php$
RewriteRule ^(.*)$ 404.php [L]
# this makes files work without .php #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]
Try this,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourfolder/index.php?/$1 [L]
</IfModule>
Try this code:
# if file has a .php extension redirect to 404 page #
RewriteCond %{THE_REQUEST} \s.+?\.php[\s?] [NC]
RewriteCond %{REQUEST_URI} !/index\.php [NC]
RewriteRule ^ 404.php [L]
# this makes files work without .php #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]

Rewrite extension if file exists, redirect if file does not exist, with htaccess

Using .htaccess, I'd like to redirect files that do not exist to a controller page, and rewrite the extension of .php files that do exist to an .html extension. If a file exists and is an .html page, I'd like it to remain the same. Every time I try to inject the rewrite rule from .php to .html, I seem to mess up the redirect to the controller page. So I'm not sure where to go from here:
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
ErrorDocument 404 /404.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
</IfModule>
Any help I'd be most grateful for.
Edit
I seem to have found most of the answer here (but I have to leave out the ReweriteBse or it doesn't work). The biggest issue is that now, my existing .html files don't work, it only serves my .php files with .html extensions and directs all else to the controller. Existing .html files go to my 404 page. I'd like to know how I can keep my existing .html files intact. My new code as follows:
RewriteEngine on
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
Try:
<IfModule mod_rewrite.c>
RewriteEngine on
# If a request for a php file is made, check that it's actually a php file then redirect the browser
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*?)\.php($|\ )
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*?)\.php$ /$1.html [L,R=301]
# If a request for an html file is made, check that it's a php file, and if so, serve the php file:
RewriteCond %{REQUEST_URI} ^/(.*?)\.html$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
# Everything else goes to the controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
</IfModule>
Try this (I have added a rewrite condition to avoid an infinite loop b yadding the parameter r=0 and testing if it exists) :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*&)?r=0(&.*)?$
RewriteRule ^(.*)\.php$ $1.html [L,R=301,QSA]
RewriteCond %{REQUEST_FILENAME} ^(.*)\.html$
RewriteCond %1.php -f
RewriteRule ^(.*)\.html$ $1.php?r=0 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
I'm sure there are better ways to do this, but the following works for my needs. The other answers provided didn't seem to work despite my attempts.
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
ErrorDocument 404 /404.php
<IfModule mod_rewrite.c>
RewriteEngine on
# Check if .html file already exists -- if so, do nothing
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^.*$ - [NC,L]
# Check if .php file already exists -- if so, rewrite extension to .html
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
# All else goes to the controller page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
</IfModule>

Categories