Hi their,
I was wondering if there is anyway we can hide file name in the URL using .htaccess ?
For example
These URLs:
example.com/home.php
example.com/welcome/contact-us.php
example.com/home/support/email_us.php
Should look like
example.com/
example.com/welcome/
example.com/home/support/
Mayooran NavamanyPosted Jan 17, 2024, 4:45 PM
Yes, you can use the .htaccess file to rewrite URLs and hide file extensions in PHP. To achieve the desired result of hiding file names in the URL, you can use the Apache mod_rewrite module. Here's a basic example of how you can accomplish this:
To create or edit your .htaccess file, you'll need access to the file system of your web server. Here are general instructions for creating or editing the .htaccess file:
How to Create a New.htaccess File:
Open a Text Editor: Use a text editor of your choice, such as Notepad on Windows, TextEdit on macOS, or any code editor like Visual Studio Code, Sublime Text, or Atom.
Write .htaccess Rules: Add the necessary rules to the file. For example, you can use the rules mentioned in the previous answer to remove file extensions and achieve URL rewriting.
Save the File: Save the file with the name
.htaccess(including the dot at the beginning) in the root directory of your website.How To Editing an Existing .htaccess File:
Locate the .htaccess File: If you already have an existing .htaccess file, locate it in the root directory of your website. If you can't find it, it might be hidden. Make sure your file explorer or FTP client is set to show hidden files.
Open the .htaccess File: Open the .htaccess file using a text editor. You can right-click on the file and choose "Open with" or open the file directly from your text editor.
Edit the Rules: Add or modify the rules based on your requirements. For example, you can use the rules mentioned in the previous answer to achieve URL rewriting.
Save the File: Save the changes to the .htaccess file.
Follow the steps by step
Create or edit your .htaccess file:
If you don't have an existing .htaccess file, you can create one in the root directory of your website. If you already have one, you can add or modify the rules accordingly.
Enable mod_rewrite:
Make sure that the mod_rewrite module is enabled in your Apache server. You can enable it by using the following command in your terminal:
Write the .htaccess rules:
Add the following rules to your .htaccess file:
This set of rules does the following:
Test your URLs:
After adding the rules, try accessing your URLs without the file extensions. For example:
example.com/home.phpshould redirect toexample.com/.example.com/welcome/contact-us.phpshould beexample.com/welcome/contact-us/.example.com/home/support/email_us.phpshould beexample.com/home/support/email_us/.Note: Make sure to backup your .htaccess file before making changes, and be aware that misconfigurations can lead to server errors. If you encounter issues, check your server logs for more information.
Jithu ThomasPosted Jan 17, 2024, 4:41 PM
You can use the Apache server's
.htaccessfile to achieve URL rewriting and hide file extensions. To remove file extensions and display cleaner URLs, you can use the following.htaccessrules:RewriteEngine On
# Remove file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
# Redirect to trailing slash for directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
This code assumes that you are using Apache with mod_rewrite enabled. Place this code in your website's root directory in a file named
.htaccess.The code is explained below: -
RewriteEngine On: Enables the URL rewriting engine.RewriteCond %{REQUEST_FILENAME} !-d: Checks if the requested URL is not an existing directory.RewriteCond %{REQUEST_FILENAME}.php -f: Checks if the requested URL with ".php" appended is an existing file.RewriteRule ^([^/]+)/?$ $1.php [L]: If conditions 2 and 3 are true, rewrites the URL by appending ".php" to the requested URL.RewriteCond %{REQUEST_FILENAME} -d: Checks if the requested URL is an existing directory.RewriteRule ^(.*[^/])$ /$1/ [L,R=301]: If condition 5 is true, redirects to the same URL with a trailing slash. TheR=301flag makes it a permanent redirect.With these rules, your URLs should appear as you specified:
example.com/instead ofexample.com/home.phpexample.com/welcome/instead ofexample.com/welcome/contact-us.phpexample.com/home/support/instead ofexample.com/home/support/email_us.php