39 thoughts on “About

  1. Alvin says:

    Hi Anubhava,

    I noticed you’re good at mod_rewrites and was hoping you can help me. I have a website that has the following structure of folders and files:

    index.php
    about.php
    contact.php
    work (this is a folder which contains a subfolder called woodwork. The woodwork folder contains 3 files: project1.php, project2.php, and project3.php)
    work.php
    services (this is a folder which contains two files: cabinets.php and tables.php)
    services.php

    What I’m trying to do is use .htaccess mod_rewrite to remove the .php extension from the url so if someone types about.php, a 404 error will be shown and if someone clicks on any of the links in the navigation menu or any link located within the site on any page, the url will default to the file name without the .php extension. This rule will apply to the subdirectory files as well, which I’m having a hard time trying to get to work. So, if someone clicks about in the menu, the url will show example.com/about.

    The menu has the following links: Home, About, Work, Services, Contact.

    If a user clicks on Work in the menu, the work.php will show. Within the work.php page, there are three links under a woodwork header called Project 1, Project 2, and Project 3. Depending on which project the user clicks on, they will be taken to either project1.php, project2.php, and project3.php. If someone clicks on Project 1 on the Work.php page, the url should read, example.com/work/woodwork/project1.

    If a user clicks Services in the main menu, the services.php page should show and the url should read as example.com/services. If the user clicks Cabinets on the Services page, then it will take them to the cabinets.php page and the url should read as: example.com/services/cabinets

    Right now, I can remove the .php extension from index, about, and contact, services, and work, however when I click on a link on the services or work page, I get a 404 because it’s trying to use the .php extension, meaning I can see the .php extension in the url when I hover over the link. For example, if I’m on the services page and I hover over the cabinets link, the url will show as example.com/services/cabinets.php. When I click the link, it will show a 404. However, if I manually type in example.com/services/cabinets, the page will load. How do I fix this? The code I’m using in my htaccess is:

    RewriteEngine On
    # Disable Automatic Directory detection
    DirectorySlash Off

    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php

    # Return 404 if original request is .php
    RewriteCond %{THE_REQUEST} “^[^ ]* .*?\.php[? ].*$”
    RewriteRule .* – [L,R=404]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s