htaccess redirect to homepage with bad url - php

I have this redirect htaccess code to change the file.php to /file/ so the URLS are more SEO friendly.
RewriteEngine On
# hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
However, when someone types in a bad url like - domain.com/file/whateverblablabla-wrong
I am getting an Internal Server Error.
I would like it to redirect to the homepage or if I choose a specific URL for a 404 page so I can have a sitemap or something maybe..
Much thanks,
-O

Try adding this line on top of your .htaccess:
ErrorDocument 404 /404.php
RewriteEngine On
# hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
This will disable any previously set ErrorDocument directives.

Related

htaccess removal of backslash

I'm sure this question has come up before but I don't really know what to search.
my .htaccess file seems to be removing the '/' when I click links externally for the site.
For example, when I click "www.mysite.com/home" I'm directed to "www.mysite.comhome"
Here's my .htaccess
# Turn mod_rewrite on
Allow from all
RewriteEngine On
Options +FollowSymLinks -MultiViews
ErrorDocument 404 /errors/404.php
DirectoryIndex home.php
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
Thanks again !!
Problem is your last rule that is attempting to string multiple slashes from URLs.
You can use these rules in your site root .htaccess:
ErrorDocument 404 /errors/404.php
DirectoryIndex home.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L,NE]
# strip multiple slashes
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /$0 [R=301,L,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Make sure to use a new browser to avoid old cache or clean your browser cache completely before testing this change.

Redirecting all URLs with extension to no extension, including in subdirectories

I'm looking for a way in which I could redirect https://example.com/foo.php to https://example.com/foo and also https://example.com/foo/ to https://example.com/foo.
There are copious solutions to this posted on Stack Overflow, but none of them transpire to address an instance where the user tries to access a file such as https://example.com/foo/bar/baz.php.
Could anyone kindly propose something to solve this with .htaccess?
The code I have currently is:
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).php
RewriteRule ^ %1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)/\s
RewriteRule ^ %1 [R=301,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
Try these rules:
RewriteEngine On
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=302,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Remember to clear your browser cache before testing.

mod_rewrite and .htaccess exception to the rule

I want to hide the .php extension throughout my site, I have this snippet and it works fine. However I have one URL that I want to be an exception to the rule, that is I want it have the .php intact and all it's $_GET params.
How can I include that exception into this snippet? And why does it work?
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=302,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
You can add exception like this:
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 !^(exception1|exception2) %1/ [R=302,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
btw this code and comment look like something I would have written it in the past :)

hide.php files with variables htaccess

i have a problem on my htaccess file , i'm trying to hide all .php extensions on my website,
i tried many htaccess codes but most of it gives me errors , 404 Not Found , most of my files contain variables like ?id and ?category , index/?cat=Action , i want htaccess to hide php extension and forward /dir/foo to /dir/foo.php , help me pls :)
RewriteEngine On
RewriteBase /
# turn off index.php for home page
RewriteRule ^index\.php/?$ / [L,R=301,NC]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
You need to write php to the end of urls not the other way around. You can write variables like this: href="foo?bar=1". This code has worked for me:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You can use this code in your root .htaccess:
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

.htaccess rewrite and remove .php on only a few files

I am trying to rewrite all my .php file extensions to /
And I got it to work wit this code:
RewriteEngine On
# hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
However, It breaks my admin pages that are talking to the database for example pages that have code and use the URL break:
create-ticket.php?device=XBOX+360
I need to leave the extension on a a couple pages...
Is there a way I can use the above code but only on specific files and not every file that ends with .php?
Thanks,
-O
You can do exclusion like this:
RewriteEngine On
# hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} !/(create-ticket|admin)\.php [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Categories