Jump to content

Mod Rewrite Regex


mbtaylor

Recommended Posts

Hi everyone

 

I use a regex that I KNOW should be better but I cant seem to get the right syntax.

Here is what I use:

 

RewriteBase /
RewriteCond %{REQUEST_URI} (\.htm)
RewriteRule    ^([^/]+)?/?([^/]+)?/?([^/]+)?/?([^/]*)$     index.php

 

What it does is basically send any HTML page to index.php - which then gets dealt with using the url as variables for content lookup etc. What I am wondering is how I repeat the middle section

 

?/?([^/]+)

 

so that it will do any url lengh as currently it does 3 levels e.g:

 

http://www.mydomain.com/dir1/dir2/dir3/somepage.htm

 

Cheers!

 

Mark

Link to comment
Share on other sites

Hmmm no that didnt work but cheers. The reason why it didnt work was because FCKEditor (inline html editor) loads the page content into itself when using your modified regex, whereas it doesnt with mine.

 

Otherwise it would have worked... I dont know whats going on there.

Link to comment
Share on other sites

For example

 

http://www.mysite.com/home.htm

http://www.mysite.com/somedir/blah.htm

http://www.mysite.com/somedir/anotherdir/blah.htm

 

The url can be anything, any depth etc. It all gets sent to index.php.

None of the urls are 'real' all content is dynamic and stored in the database.

 

So I guess the real url would be something like http://www.mysite.com/1/2/3

Link to comment
Share on other sites

I'm not sure why the previous expression didn't work. The URIs from your example would be..

home.htm

somedir/blah.htm

somedir/anotherdir/blah.htm

...which match in code. Right?

 

If you want everything rewritten except links to index itself, you can try ^(?!index\.php), assuming that negative look aheads work in mod_rewrite.

Link to comment
Share on other sites

The regular expression just sends ALL urls that come to the domain to index.php for processing. Its part of a CMS framework I wrote...

 

The inline editors seem to load the whole page html into their modified textarea, I am guessing being more specific with the regular expression is what is preventing the editor loading the page html. I guess they might be replacing the textarea with inline iframe dynamically with javascript. Then maybe the iframe is loading the page content when it shouldnt be.

 

That doesnt happen with my current regex, but it does if I use varients - its something that has bothered me for quite a while!

 

An easy solution would be to make the admin sections not use mod_rewrite :)  For example, currently the admin page would be say: http://www.someclientsite.co.uk/admin.htm

 

That admin.htm doesnt exist, and is really database content being pulled from a) the menu table and b) the content table. The admin.htm is matched to a pageID and the appropriate xhtml content displayed along with whatever code modules etc that relate (admin ones in this case).  All page urls are really just virtual urls and are instead of doing something like:

http://www.clientsite.co.uk/index.php?pageID=123 etc.

 

If I was to use another specific php page like admin.php (only .htm pages use mod_rewrite in the condition) then I would be fine, but then, that would mean changing system programming :P

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.