I am working on a project where I decided to get rid of the .php extension from the URL of my app. I am successful using the htaccess code given below. But the problem is, the page still loads using .php extension if typed manually or loaded from the history.
My current .htaccess code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I want it to always load with www.example.com/dashboard except www.example.com/dashboard.php even if dashboard.php is manually typed.
UPDATED .htaccess code
RewriteEngine On
RewriteBase /dateapp/
RewriteRule ^([^\.]+).php$ $1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You can add redirect as first rule.
RewriteEngine On
RewriteRule ^([^\.]+).php$ /$1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You have to use END flag instead of L otherwise the redirects will end in infinite loop. This will work if the .htaccess is in your webroot folder.
In case you have .htaccess in 'folder' subfolder you will have to add RewriteBase
RewriteEngine On
RewriteBase /folder/
RewriteRule ^([^\.]+).php$ $1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You may use this code in dateapp/.htaccess:
RewriteEngine On
RewriteBase /dateapp/
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Related
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]
I have issue on url rewrite i am want to change url
http://www.example.com/mbl.php?domain=example.com to http://www.example.com/mbl/example.com
So i have written code myself by as below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^/mbl/([^/]+)$ /mbl.php?domain=$1
But its not working. so please to my error.
Thanks
You should try your rules this way and remove the leading slash from the rewriterule with mbl since you are using it in .htaccess file.
Give these rules a try.
#Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^mbl/([^/]+)$ /mbl.php?domain=$1 [L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have a little problem accessing index files when rewrite engine removes .php and .html...
I have an index in "public_html/user/username/index.php"
But it throws me to error page when I'm trying to access either
mydomain.com/user/username or
mydomain.com/user/username/index or
mydomain.com/user/username/index.php
When I'm trying to reach any file from folder in public_html folder let's say
public_html/user/username.php == mydomain.com/user/username <-- WORKS
What is going on?
My .htaccess :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Rewrite engine is already on and everything works perfectly...
You should check for presence of php and html files before adding those etenstions:
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]
Here is my files
index.php
blog.php
about.php
I am accessing it as
localhost/index.php or localhost
localhost/blog.php
localhost/about.php
How can i rewrite .htaccess so that i can access it by
localhost/
localhost/blog
localhost/about
Update
My image are not displaying once i applied the below provided .htaccess, can any give fix this ?
Simple try this in .htaccess file :
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.html
Options -MultiViews # optional Depends on Server
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
EDIT:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This is my .htaccess file, everything works so far but I can't manage to remove .php extension from files, every code that I tried from other answers just threw 500 or 404 error. Please advise where and what to add. Structure of the folders is localhost/myfolder/somefile.php
Just to be clear - localhost/myfolder/ is a root for my project.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News
RewriteRule ^product-news/([0-9]+[/])?$ posts.php? p=$1&cat=Product\ News
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1
This snippet will allow you to rewrite to remove php extensions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want your URL to have a trailing /, you can use this snippet:
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]
Source
Removing extension, say; php or html in browser will let browser find it a little bit more effort to find the source file. if you need it so, this follows may help you:
UPDATED:
PHP:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/$ /$1.php
HTML:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/$ /$1.html
those will remove all extensions in your files (both php and html).
Note: see if server enables mod rewrite module/extension.
You should be using two .htaccess files. The first should go in your localhost root (to redirect requests to myfolder), and the second should go into myfolder (to match up routes against your PHP files):
Root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
myfolder .htaccess:
RewriteEngine On
RewriteBase /myfolder/
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News [L]
RewriteRule ^product-news/([0-9]+[/])?$ posts.php?p=$1&cat=Product\ News [L]
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials [L]
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs [L]
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1 [L]
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1 [L]
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1 [L]
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1 [L]
Note how I have also included [L] to stop it from doing anything else once it has found a match.
Just below your 301 rule add this rule:
RewriteEngine On
RewriteBase /myfolder/
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]