htaccess prevent page extensions displaying - php

I have my htaccess file set up so that my website pages don't need to display an extension, my links are setup like this, with the slash at the end:
www.mywebsite/about/
These links work - But if I were to directly type in
www.mywebsite/about.html
The website will show this page with the html extension. Is there a way that I can prevent extensions from showing even if they are directly typed in? :S
The majority of my pages are html, except for one php page. This is what I have in my htaccess:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteRule ^php-page/([^/\.]+)/?$ php-page.php?p=$1 [L]
If anyone can help me out with this I would really appreciate it! :)

Please try this:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(.*)\.html$
RewriteRule .* %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteRule ^php-page/([^/\.]+)/?$ php-page.php?p=$1 [L]
I think it can be done a bit better than this, but it should work.

Related

Issue with .htaccess for friendly urls

This is my currently my .htaccess file
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
RewriteRule ^(.*)/edit/(\d+)?$ $1/edit.php?tag=$2 [NC]
As you can see it's set to hide the .php extension, but this then seems to break the edit rule. If I comment out
RewriteRule ^(.*)$ $1.php [NC]
The edit rule works fine, but I need both and cannot seem to get it to work, anyone see what the problem is and how to sort it.
[Edit]
I have a link like this and when all rules are active this is what doesn't work.
http://www.domainname.com/researcher/lists/edit/
and I get an 500 Internal Server Error.
Try rules as in your site root .htaccess:
Options -MultiViews
RewriteEngine on
RewriteRule ^(.+)/edit(?:/(\d+))?/?$ $1/edit.php?tag=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Removing file extensions & redirecting to index page

So I am trying to get my site to remove all file extensions so instead of https://multimap.xyz/error/id.php?=... it will just be https://multimap.xyz/error/id?=... . Simarly I should be able to do https://multimap.xyz/mail/index.php and it removes the trailing index.php to result in just https://multimap.xyz/mail/.
I'm trying to achieve this with both .php and .html file extensions and indexes and using .htaccess and this is what I have so far
php_flag display_errors off
ErrorDocument 404 https://multimap.xyz/error/id?=404
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
This works well for removing file extensions. So the .../error/id?=... works perfectly. However when trying to access https://multimap.xyz/mail/ it shows me an error page saying
The requested URL /email/.php was not found on this server.
Any help/suggestions are appreciated.
Thank you :)
You should check presence of .html and .php file before their rewrites.
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))\.(?:php|html)[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

htaccess adding .php to the end of $_GET variables when adding slash

I attempted to create a little bit of htaccess which alters a URL from something like
http://localhost/website/page.php?id=_abc-123
to
http://localhost/website/page/_abc-123
It works for the most part, in that I can visit the page without having trouble locating scripts and CSS files. However, if I try to echo out $_GET["id"], instead of getting _abc-123, I will get _abc-123.php.
This is what I have so far within my htaccess file:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
All help is appreciated,
Thanks.
Test movie page first, and test if file with .php exists:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
# remove extensions
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
First of all, if you are not sure where your error lies, you can try online tools for .htaccess like htaccess.mwl.be.
Obviously your first RewriteRule contitions are met, which results in your "error".
With the help of this tool and some knowledge about how regex work, we can fix your .htaccess to this:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
The only thing I changed is removing the "L" flag from your first RewriteRule, because its RewriteCond is met but we need it to go through the second RewriteRule.
For more information about the L-flag have a look at the documentation.

Error on url rewrite .htaccess

Actually, I'm not fluent in the use. Htaccess file, the following lines of code are fetched me from using the internet. But now it gets some unexpected error.
The first bug: when I entered http://localhost/giccos/wall/username (pretty good showing and error but when I add it to the rear .php moment it into http://localhost/giccos/wall/username?username=username and if I can fix it to remain http://localhost/giccos/wall/username?username=usernames2312312s page this time)
How do I fix this? I want it kind of like facebook, if we add .php normal operation.
And here is my mess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /giccos/
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^.*$ - [L]
RewriteRule ^status/(.*).php status.php?status_id=$1
RewriteRule ^hashtag/(.*).php hashtag.php?hashtag=$1
RewriteRule ^wall/(.*).php wall.php?username=$1
RewriteRule ^blog/(.*).php blog.php?url=$1
RewriteRule ^verify/user/(.*).php verify.php?user=$1
RewriteRule ^groups/(.*).php groups.php?groups_id=$1
#RewriteRule ^life/(.*) $1.php
RewriteRule ^life/ newsfeed.php
DirectoryIndex home.php
Options All -Indexes
## Auto remove .PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

htaccess rewrite url to hide php question mark

my url formatmysite/profile/?theusernamewith hide php extension, im trying to hide question mark in url so the url will like mysite/profile/theusername, looked up few posts about what i should do is add external redirect then internal forward in htaccess, tried lot of code still can't get it work. this is what i have now:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Keep your .htaccess like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /profile/
# new rules
RewriteCond %{THE_REQUEST} \s/+profile/?(?:index\.php)?\?([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?$1 [L,QSA]
# php hiding
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Categories