?
Solved

Better URL-Rewrite RegEx for .htaccess0

Posted on 2007-12-05
7
Medium Priority
?
345 Views
Last Modified: 2010-04-21
For a while I've been using the same .htaccess file when I create a new site. It puts 3 variables in the URL (so far i haven't needed more than 3) and uses the clean URL format that is so popular. I don't remember where I got this but I think it's time to update it. I tried to enter a url with a "-" (like "/aug-sept/") and it tried looking for the real file, and my script didn't catch it. It also broke with "/aug_sept/".

So what i'm looking for is to replace these RegEx strings that are supposed to get the URL variable with some that will accept any normal character in a standard URL. There may also be a better, more flexible way of doing this, but I really don't have the knowledge to understand how to modify it.

BTW, in the code below, variable 4 is supposed to catch anything else in the URL, but that doesn't seem to really work. I would REALLY like this script to work with ANY file OR folders, for example, all of the following would point to the same content:

/contact/email/
/contact/email.php
/contact/email.html
/contact/email.aspx
/contact/email

Does that make sense?
RewriteEngine on
 
RewriteRule		^$							index.php [L]
RewriteRule		^([a-zA-Z0-9\-\_/]*)/([a-zA-Z0-9\-\_/]*)/([a-zA-Z0-9\-\_/]*)/([a-zA-Z0-9\-\_/]*)/$		index.php?action=$1&item=$2&option=$3&$4 [L]
RewriteRule		^([a-zA-Z0-9\-\_/]*)/([a-zA-Z0-9\-\_/]*)/([a-zA-Z0-9\-\_/]*)/([a-zA-Z0-9\-\_/]*)$		index.php?action=$1&item=$2&option=$3&$4 [L]
RewriteRule		^([a-zA-Z0-9\-\_/]*)/([a-zA-Z0-9\-\_/]*)/([a-zA-Z0-9\-\_/]*)/$		index.php?action=$1&item=$2&option=$3 [L]
RewriteRule		^([a-zA-Z0-9\-\_/]*)/([a-zA-Z0-9\-\_/]*)/([a-zA-Z0-9\-\_/]*)$		index.php?action=$1&item=$2&option=$3 [L]
RewriteRule		^([a-zA-Z0-9\-\_/]*)/([a-zA-Z0-9\-\_/]*)/$		index.php?action=$1&item=$2 [L]
RewriteRule		^([a-zA-Z0-9\-\_/]*)/([a-zA-Z0-9\-\_/]*)$		index.php?action=$1&item=$2 [L]
RewriteRule		^([a-zA-Z0-9\-\_/]*)/$		index.php?action=$1 [L]
RewriteRule		^([a-zA-Z0-9\-\_/]*)$		index.php?action=$1 [L]
 
#this restricts access to this file
RewriteRule		^\.htaccess$				- [F]

Open in new window

0
Comment
Question by:bdichiara
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 18

Accepted Solution

by:
Mark Gilbert earned 1500 total points
ID: 20421937
If it's all to point to the same content then simply do this:

RewriteCond   %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule   ^(.+)                             mynewpage.php

That should then make everything, no matter what the slashes contain etc, to point to the php file.  If you are looking for something other than that let us know what you are expecting and the appropriate code can be provided.  Hope this helps.
0
 
LVL 7

Author Comment

by:bdichiara
ID: 20423399
That sounds kinda right, but i want there to be 3 (or more) variable sent across. For example:

http://www.mysite.com/contact/
works as if it were:
http://www.mysite.com/index.php?action=contact

http://www.mysite.com/contact/email/
works as if it were:
http://www.mysite.com/index.php?action=contact&item=email

My concern with your example is I would somehow have parse through the REQUEST_URI to get what variables appear after domain name, so that I know what content to show the user.

I know a lot of URL Rewrites work similar to this:

http://www.mysite.com/action/contact/item/email/
works as if it were:
http://www.mysite.com/index.php?action=contact&item=email

However, I already want the first 3 items to be set to specific variables.

I hope that clears it up a bit.
0
 
LVL 7

Author Comment

by:bdichiara
ID: 20436074
Ok, I actually think I might have it figured out.

Thanks for the help.
RewriteEngine on
 
RewriteRule		^$			index.php [L]
RewriteRule		^\?([^/\.]+)$			index.php?$1 [L]
RewriteRule		^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/$		index.php?action=$1&item=$2&option=$3&$4 [L]
RewriteRule		^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)$		index.php?action=$1&item=$2&option=$3&$4 [L]
RewriteRule		^([^/\.]+)/([^/\.]+)/([^/\.]+)/$		index.php?action=$1&item=$2&option=$3 [L]
RewriteRule		^([^/\.]+)/([^/\.]+)/([^/\.]+)$		index.php?action=$1&item=$2&option=$3 [L]
RewriteRule		^([^/\.]+)/([^/\.]+)/$		index.php?action=$1&item=$2 [L]
RewriteRule		^([^/\.]+)/([^/\.]+)$		index.php?action=$1&item=$2 [L]
RewriteRule		^([^/\.]+)/$		index.php?action=$1 [L]
RewriteRule		^([^/\.]+)$		index.php?action=$1 [L]
 
#this restricts access to this file
RewriteRule		^\.htaccess$				- [F]

Open in new window

0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 18

Expert Comment

by:Mark Gilbert
ID: 20439660
Hi bdichiara, glad I was able to assist you in some way. The code you posted looks correct.  You may want to impliment at least the first two lines I posted just in case you are trying to load something other than index.  For example you may have a page called help.php which you may want called, without going through index.  The first two lines check to see that help.php exists first, and if it does, go to it, otherwise go to the clauses in your index.php.  Hope this helps.
0
 
LVL 7

Author Comment

by:bdichiara
ID: 20446684
thanks. I actually did that, but is there a more functional way. For example if they type in help.php, rather than just sending them to the index if the file doesn't exists, can i get it to try to send them to /help/ ? Then my other rules will catch it and it will forward them to my custom 404 if /help/ is not in my database.

Thanks.
0
 
LVL 18

Assisted Solution

by:Mark Gilbert
Mark Gilbert earned 1500 total points
ID: 20449447
Hi Bdichiara,

what I would recommend is putting the help clause before the first index clause:

RewriteRule            /help.php                  /help [L]
RewriteRule            ^$                  index.php [L]


Although your htaccess file will be quite large, you are directing your traffic to various parts of your site based on certain conditions.  

Hope this helps.
0
 
LVL 7

Author Closing Comment

by:bdichiara
ID: 31413091
The first response sort of worked, however it affected my CSS and IMG (jpeg, gif, etc) files. I think I will just stick to what I posted. The 2nd helped, but to not clutter up my .htaccess file any more than it already is, i'll just accept a 404 for some of those pages. Thanks.
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question