How to create a simple but flexible Clean URL in .htaccess with Apache Mod_rewrite?

I have just started to be familiar with Apache Mod_rewrite and I have falied to achieve my goal.

I am building a website where I'd like to replace the ugly URLs with Clean, user firendly URLs. I have currently created some files already, but I'm not sure if I'll create clean urls, I'll be needing meny of these pages.

I'd have 2 questions, on how would professionals create this website, here we go:

1st question:

I have the following file structure which you see below:

Main folder:

  • index.php (Home Page)
  • login.php (Login Page)
  • register.php (Register Page)

    Under Main Folder I have: Members folder:

    • index.php (Dashboard Page)
    • stats.php (Statistics Page)

The first problem/question is the following:

I'm currently just using the simple .php pages to access it's content. Would it be better to use 1 index.php file and include the necessary files when someone enters the following for example: /index.php?go=login So would this be more professional than just providing the .php files and deliver their content when they're clicked?

2nd question:

This question would be around the Clean URLs. I would like to achieve the following:

When someone visits: http://www.example.com/index.php - it should redirects them to http://www.example.com/

When someone visits: http://www.example.com/login.php - it should redirects them to http://www.example.com/login/ - if someone types http://www.example.com/login that should also be processing login.php

As well as the other pages, like:

http://www.example.com/register.php <-> http://www.example.com/register/ (the slash should be optional) http://www.example.com/members/dashboard.php <-> http://www.example.com/dashboard/ (the slash should be optional)

And so on...

I have already tried to create a rewrite condition, but it doesn't seems to work. Can anyone please update me on this?

What I've tried:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^login/?$ login.php [NC]

I really hope, that someone could help me to answer both of these questions. I have been searching for 2 days now, and I couldn't figure out what rewrite rule should I be using. Thank you really much!

EDIT:

Should I use a basic redirect and take care of the other codes using PHP? Please someone help me!

Answers


This (untested) code should take something.php and redirect to /something.

Additionally, it should allow you to keep doing stuff like /search?query=foo, which will be the equivalent of /search.php?query=foo.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(test|login|whatever)/?$ $1.php/$2 [NC,L]

Need Your Help

React component this.props is always empty

javascript reactjs reactjs-flux react-router react-router-component

I've followed a couple of examples in an attempt to get access to a parameter from a Route in the React component that handles it. However the result of console.log on this.props from inside the re...

Laravel5: Many to many relationship

javascript php jquery mysql laravel

I have just started laravel5, but i haven't seen the model folder just like laravel4.