I'm developing a website using PHP. I'm trying to hide any PHP extension, using .htaccess.
This is my link urls for visiting an user profile :
<a class="nav-link" href="user/<?php echo"$username"; ?>"><i class="fa fa-user ispace"></i><?php echo"$username"; ?></a>
and this is my .htaccess rules:
RewriteRule ^user/(.*)$ profile.php?user=$1 [L,QSA]
But when I click on an user profile link, my bootstrap isn't loaded.
Also, the user header link redirects to www.mysitename.com/user/home and then shows a 404 error. How to fix this?
Thanks !
To hide the .php extension:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
To hide the .html extension:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Put the .htaccess on the main Folder.
To test just do something like:
Test go to .php file
Should work.
Related
I have a website with a menu. When clicking on any of the pages in the menu it should go to the particular page whether it is page.php or page.html but it should not show the .php and .html in the url. How can I achieve this?
I have written a piece of code in htaccess. But it is not working.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
This isn't really a php question, but rather an apache question.
You have the right idea in that you need to use the .htaccess file for this.
However, don't forget to make sure that the RewriteEngine is set to ON, and to have the parameters at the end inside [ ] brackets.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can read more about this here
In my site I removed .php extension from the url using .htaccess rules. So that I can go to any page without .php extension. e.g :
http://localhost/aponit/dev/zones (there are no .php extension)
Now,I have a link in a php page to update a form data. Link is bellow :
<a class="btn btn-success btn-xs" href="<?php echo SITE_URL."update/$zone_id"?>" >Edit</a>
This link showing following url :
http://localhost/aponit/dev/update/54
But unfortunately It's showing me error message :
Internal Server Error
I am using following .htaccess rules :
ErrorDocument 404 /not-found.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^update/([0-9]+) update?z=$1 [L]
Trying to:
ErrorDocument 404 /not-found.php
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]
And post detail log file???
The .htaccess code that yuo have provided will not work correctly if the site does not run under root domain. For example
RewriteRule ^update/([0-9]+) update?z=$1 [L]
This code block tells the server that any URI starting with "update" following a number will be rewritten. but in your case, the URI always starts with "aponit/dev/". So this code will not work any time.
Solution
Set up a virtual host. You will get a lot of available tutorials online how to setup a virtual host based on your server software(XAMPP/WAMP etc)
If you just want to remove .php from url, use the following code in .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have following file in root directory :
Dev (root)
update.php
zones.php
This zones.php file is showing using following url:
http://localhost/aponit/dev/zones (I hide .php extension using .htaccess rules)
In zones.php file I have a edit link to edit a form via query string. The link is bellow :
<a class="btn btn-success btn-xs" href="<?php echo SITE_URL."zones/update?z=$zone_id"?>" >Edit</a>
When I click on this link it's showing following url :
http://localhost/aponit/dev/zones/update?z=55
But in browser it's showing me error message :
Internal Server Error
Because my .htaccess rules is not define appropriate rules for that desired link.
What I want now :
I want the url should be user friendly. E.g
http://localhost/aponit/dev/zones/update/55
How can I create this link using .htaccess ?
Current .htaccess rules :
ErrorDocument 404 /not-found.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
You can have another rule to handle zones/update/55:
Options -MultiViews
ErrorDocument 404 /not-found.php
RewriteEngine on
RewriteRule ^(?:zones/)?update/(\w+)/?$ update.php?z=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
I screwed up my .htaccess` file and now my links are messed up.
for example www.cardler.com/contact.php changes to www.cardler.com/awstats/contact.php
if you manually type in cardler.com/contact.php it works...
Link: www.cardler.com/index.php - try it out by clicking on the contact button.
.htaccess
RewriteEngine on
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
I have a website in PHP, and I'm trying to hide the extension. I've found a couple of things via Google, but they all seem to be too complicated, or they redirect index to index.php: like if you write name.com/contact it goes to name.com/contact.php. I don't want that, I just want:
www.name.com/es/index.php to be www.name.com/es/inicio
www.name.com/es/empresa.php to be www.name.com/es/empresa
www.name.com/es/productos.php to be www.name.com/es/productos
www.name.com/es/clientes.php to be www.name.com/es/clientes
www.name.com/es/recetas.php to be www.name.com/es/recetas
www.name.com/es/contacto.php to be www.name.com/es/contacto
www.name.com/en/index.php to be www.name.com/en/home
www.name.com/en/company.php to be www.name.com/en/company
www.name.com/en/products.php to be www.name.com/en/products
www.name.com/en/clients.php to be www.name.com/en/clients
www.name.com/en/recepis.php to be www.name.com/en/recepis
www.name.com/en/contact.php to be www.name.com/en/contact
the /en/ folder is English version and the /es/ folder is the Spanish version
Also the links inside the website, would be also without the extension.
E.g, an image linked to clientes would be clientes and not clientes.php and if somebody tries to go to /page.php, it still works but it redirects to /page.
The htacces file should look like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You redirect any php file in the url with the same name withouth the "php".
Then in your php files, you can check to see if the url contains the extension (http://blabla.com/bla.php) and redirect the page to the same one withouth the extension.
So, at the beginning of each php file you should call this function :
function redirectIfNeeded(){
$url = $_SERVER["REQUEST_URI"];
if(preg_match("/\.php/$", $url))
header("Location: ".preg_replace("/\.php/",$url));
}
Please crate a .htaccess file in root directory an use this code!!
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
Hope!! it would be useful for someone!
RewriteEngine on
Rewritecond %{REQUEST_URI} !-f Rewritecond %{REQUEST_URI} !-d Rewritecond %{REQUEST_URI} !-l RewriteRule ^([\w\d-]+)$ $1.php [L]
RewriteRule ^([^/.]+)$ $1.php [L]