John Davidson

.htaccess - how to hide the complete path in php with htacces file


I have a project with the name of social app which contains has a structure like this:

  • includes

    • _login.php

    • _profile.php

  • pages

    • login.php

    • profile.php

currently if I want to vists any page like profile.php I have to visit localhost/social-app/pages/login.php but I want to modify it and change the url to localhost/social-app/login.php. basically I want to get rid of the pages and make my URL a bit cleaner

My Answer:
To achieve this, you can use the following code in your .htaccess file:

RewriteEngine On
RewriteRule ^login.php$ pages/login.php [L]
RewriteRule ^profile.php$ pages/profile.php [L]

This code will rewrite the URLs so that when you visit localhost/social-app/login.php, it will actually load the pages/login.php file. Similarly, when you visit localhost/social-app/profile.php, it will load the pages/profile.php file.

Make sure to place the .htaccess file in the root directory of your project (in this case, the social-app folder). Also, ensure that the mod_rewrite module is enabled on your server for this to work.

After adding this code to your .htaccess file, you should be able to access your pages using the cleaner URLs localhost/social-app/login.php and localhost/social-app/profile.php.

Rate this post

3 of 5 based on 6927 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us