0

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

Aihsan Majeed
  • 135
  • 1
  • 11
  • Possible duplicate of [URL rewriting with PHP](http://stackoverflow.com/questions/16388959/url-rewriting-with-php) – Dan H May 14 '16 at 11:31

1 Answers1

3

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

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

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

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • I've tested this by placing the code above in a new htaccess file in the root folder on my Mamp server but I get the ' internal server error'. Have I attempted this in the wrong way? Sorry, this is my first time setting up .htaccess – Aihsan Majeed May 14 '16 at 11:55
  • Is the rewrite module/mod-rewrite enabled on your server? – Amit Verma May 14 '16 at 12:06
  • i have just enabled mod-rewrite, and I'm still get the same error – Aihsan Majeed May 14 '16 at 12:18
  • Please check your server error log for information about the error – Amit Verma May 14 '16 at 12:30
  • This is the error it shows [Sat May 14 13:33:23 2016] [error] [client 192.168.0.10] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace. – Aihsan Majeed May 14 '16 at 12:34
  • you are getting a rewrite loop error, this happens when an url rewrits to itself or rules are not in order.post your full htaccess in the question. – Amit Verma May 14 '16 at 12:44
  • I cleared all PHP defines, and I've added the s on pages, now it works a treat. Thanks.. How do i redirect the pages/page1/ to page1 – Aihsan Majeed May 14 '16 at 13:21
  • You're a star. Where the best place to learn all .htaccess coding – Aihsan Majeed May 14 '16 at 14:12