1

I'm sure this has been posted somewhere else but I can't find it. I have spent a few hours researching and trying everything I can thing of from sample code to generators. I have a site that is currently residing in a subfolder called "new". I will be moving the site to the root folder once it is ready for launch. The site is database driven and right now to access the pages you need to use a url like this:

http://domain.com/new/index.php?page=about

I would like to have it so the URL looks like this:

http://domain.com/new/about/

I have tried so many different things I don't know where to start. Here is the PHP code in index.php I use:

$page = (isset($_GET['page']) ? $_GET['page'] : 'home');
$data = get_data($page);

The get_data() simply grabs the content from the database based on the variable that was passed to it in the URL. Yes, everything is properly escaped in the function before querying the database.

Here is my current .htaccess file:

Options +FollowSymLinks
RewriteEngine On

RewriteBase /new/

RewriteCond %{REQUEST_FILENAME} -f [NC,OR] 
RewriteCond %{REQUEST_FILENAME} -d [NC] 
RewriteRule .* - [L]

RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?page=$1

Oddly enough /new/home/ works but any other like /new/contact-us/ does not work. The only thing I can think of is /home/ is the only one without a '-' character in the url. Am I missing something in my .htaccess to account for this?

GrumpyToaster
  • 335
  • 2
  • 12
  • `^([a-zA-Z0-9]+)$` does not match `contact-us`. You can match everything with: `^(.*)$` and have your PHP script deal with trying to find a match for it. – Halcyon Jun 08 '15 at 13:01

3 Answers3

3

Oddly enough /new/home/ works but any other like /new/contact-us/ does not work. The only thing I can think of is /home/ is the only one without a '-' character in the url. Am I missing something in my .htaccess to account for this?

You are absolutely missing something to account for the dash. The regular expression you are using --

^([a-zA-Z0-9]+)$

Uses a character range that does not include the dash. Let's break down the regular expression by part:

  • ^ anchors the match to the beginning of a string
  • ( begins a submatch grouping
    • [ begins a character class -- this one is [a-zA-Z0-9]+, which matches three distinct ranges one or more times; the ranges specified are:
      • a-z matches lowercase alphabetic characters abcdefghijklmnopqrstuvwxyz
      • A-Z matches uppercase alphabetic characters ABCDEFGHIJKLMNOPQRSTUVWXYZ
      • 0-9 matches numeric characters 0123456789
    • ] ends the character class
    • + specifies that the character class should match one or more times
  • ) ends the submatch group
  • $ anchors the match at the end of the string

Note that none of the ranges you've specified in your character class include the dash. To also match the dash, you need to include the dash in the character class, like so:

^([a-zA-Z0-9-]+)$

For more information on character classes, the entry on regular expressions in Wikipedia has a good bit of information.

diz
  • 275
  • 3
  • 9
2

Try this: [a-zA-Z0-9_-.][\w.]+

Take a look at this answer for more information.

Community
  • 1
  • 1
Sotiris Kiritsis
  • 3,178
  • 3
  • 23
  • 31
2

Let's focus on your regexp

RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?page=$1

Here you rewrite all that match the pattern :

Start with letter (Capitalized or not or number ) but you don't include "-" in the regexp.

You could then try that :

RewriteRule ^([a-zA-Z0-9-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?page=$1

But if you do that you allow url like that : index.php?page=-------

if you just want url like contact-us

RewriteRule ^([a-zA-Z0-9]+(-[A-Za-z])*)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9]+(-[A-Za-z])*)/$ index.php?page=$1

Which allow url like that :

index.php?page=ABaC8 or index.php?page=ABnC9-AIeE

For exemple.

kaldoran
  • 845
  • 6
  • 19