Aihsan Majeed Aihsan Majeed - 6 months ago 14
PHP Question

simple URL rewrite using htaccess

I'm looking to rewrite my current directory structure to create a friendly URL.

Directory Structure

Root Folder


  • includes

  • images

  • css

  • js

  • partials

  • pages

  • Page 1


    • index.php


  • Page 2

  • Page 3

  • Page 4



So the current URL path is www.example.com/page/page1, but what i would like is to have is www.example.com/page1. So the website can be placed Bitbucket to enable versioning.

Any help or links are hugely appreciated.

Thanks

Answer

You can use the following rule in root/.htaccess :

RewriteEngine on
#1) Redirect from "/page/foobar" to "/foobar" #
RewriteCond %{THE_REQUEST} /page/(.+)\sHTTP [NC]
RewriteRule ^ /%1 [L,R]
#2)internally redirect "/foobar" to "/page/foobar" #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /page/$1 [NC,L]

This will internally redirect /page to /page/page .

Comments