How to remove .php .html via .htaccess? - .htaccess

i develop to remove .html .php, http://webrivers.co.in/joojmail_inbox this link work fine but cannot work subfolder http://webrivers.co.in/joojmail_inbox/joojdrive/, what i miss it.
my.htaccess code
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://webrivers.co.in/error/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://webrivers.co.in/error/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
# Redirect external .html requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.html$ http://webrivers.co.in/error/$1 [R=301,L]
# Resolve .html file for extensionless html urls
RewriteRule ^([^/.]+)$ $1.html [L]
<IfModule mod_rewrite.c>
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /error/index.php [L]
</IfModule>
RewriteCond %{HTTP_USER_AGENT} ^HTTrack [OR]
RewriteCond %{HTTP_USER_AGENT} ^SiteSucker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebZip [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebCopier [OR]
RewriteCond %{HTTP_USER_AGENT} ^Zeus
RewriteRule ^.*$ no_download.html [L]
Please solve my problem.,

The following code in your .htaccess file should take care of this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
or see this article

Related

URL rewrite when .php extension is already hidden

I have a URL that has already been rewritten to hide my .php extensions. I am now having issues when trying to further rewrite the URL.
My current URLs (note the forward slash before the query string):
https://example.com/card2/?search=Jimmmy
Here is what I have currently tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?card2/(.*?)/?$ /card2.php?search=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /card2.php\?search=([^\&\ ]+)
RewriteRule ^/?card2\.php$ /card2/%1? [L,R=301,NE]
This works if I try this URL (I have to manually remove the forward slash before the query string and type in .php):
https://example.com/card2.php?search=Jimmy
So the only way I can get it to work at the moment is by typing in .php in the URL and removing the forward slash after that.
I have tried the following, my logic was to just remove the .php extensions that were in the rewrite and add the forward slash too but it doesn't work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?card2/(.*?)/?$ /card2/?search=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /card2\/\?search=([^\&\ ]+)
RewriteRule ^/?card2$ /card2/%1? [L,R=301,NE]
EDIT, thanks to anubhava for the answer, here is my htaccess file now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
ErrorDocument 404 /not-found.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
<filesMatch "\.( jpg|jpeg|gif|png|ico|js)$">
Header set Cache-Control "max-age=2419200, public, must-revalidate"
</filesMatch>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
deny from 70.24.57.210
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?card2/(.*?)/?$ /card2/?search=$1 [L]
RewriteCond %{THE_REQUEST} \s/card2(?:\.php)?/?\?search=([^\&\s]+) [NC]
RewriteRule ^ /card2/%1? [L,R=301,NE]
Have your redirect rule like this with optional matches:
RewriteCond %{THE_REQUEST} \s/card2(?:\.php)?/?\?search=([^\&\s]+) [NC]
RewriteRule ^ /card2/%1? [L,R=301,NE]
Also keep this this rule at top of your .htaccess and test from a new browser.

HTACCESS Rewrite, remove .php and redirect working for one rule and not the other

I have the following example urls, there are many possible parameter values
http://www.example.com/area-codes.php?code=0000
http://www.example.com/search.php?number=00000000000
Which I wish to rewrite to the following, respectively.
http://www.example.com/area-codes/0000
http://www.example.com/number/00000000000
Currently, the second url is rewriting and redirecting perfectly.
However the top one is causing an Internal server error
Here's my htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} \.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^ - [L]
#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]
RewriteRule ^area-codes/([^/]*)$ /area-codes.php?code=$1 [L,QSA]
RewriteRule ^number/([^/]*)$ /search.php?number=$1 [L,QSA]
I am assuming there's something wrong with my flag?
Commented out these two lines and it's worked.
#resolve .php file for extensionless php urls
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

Removing index.php from subdomain

I have the following .htaccess file in my subdomain.
My website has http://hello/forum/index.php, and would like to hide this index.php file, and make it http://hello/forum.
Whenever I use the attached .htaccess, it directs to http://hello, not http://hello/forum. Am I missing something?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) forum/$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ forum/index.php/$1 [L]
</IfModule>
Try these rules:
RewriteCond %{THE_REQUEST} ^GET\ /(forum)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /forum/index.php/$1 [L]
I was able to solve this issue by adding
DirectoryIndex index.php index.html
in .htaccess file.

.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>

.htaccess how can I implement both "vanity url" and "no extension"

I'm using htaccess from stackoverflow.com/q/8583856 -
RewriteEngine on
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/profile.php?u=$1 [NC]
Everything works great unless I type www.mysite.com into address bar -
returns mysite.com/profile?u=index.html.var
which errors "Unknown column index.html.var in where clause"
Anyone know how to get this to go to mysite.com/index instead?
Change your code to this and let me know how it behaves (after clearing your browser cache):
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=302,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ profile.php?u=$1 [NC,L]
Once you're sure it is working then change R=302 to R=301
try adding a condition to check if the path is empty (i.e. root)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond $0 ^$
RewriteRule .* - [L]

Resources