Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-01-28 14:21:10

zero
Member
From: Lancashire
Registered: 2004-04-19
Posts: 1,470
Website

Regular Expression needed for reWrite

I need a regular expression that will capture any title with any number of letters or numbers or hyphens in any order, but which will reject any title with / or ? in it.

My site presently has /title as the url mode but I want to change this to /section/title.

I need the regular expression to prevent 404s and so I can redirect visitors and robots permanently from
http://mysite.com/example-title-1
to
http://mysite.com/section/example-title-1

I’ve been trying to figure this out for some weeks now and also tried plugins but so far without success, so I will be most grateful if someone can answer this.


BB6 Band My band
Gud One My blog

Offline

#2 2007-01-28 15:11:02

hcgtv
Plugin Author
From: Key Largo, Florida
Registered: 2005-11-29
Posts: 2,722
Website

Re: Regular Expression needed for reWrite

Have you tried it this way?

RedirectMatch 301 ^/(.*)$ http://mysite.com/section/$1

What problems arise with / or ?

Offline

#3 2007-01-28 16:56:11

zero
Member
From: Lancashire
Registered: 2004-04-19
Posts: 1,470
Website

Re: Regular Expression needed for reWrite

I’ll try that Bert, but I think that when someone types in
http://mysite.com/section/title
they will get a repeating loop such as:
http://mysite.com/section/section/section/section etc.
That’s why I don’t want it to pick up any / other than the one immediately after mysite.com


BB6 Band My band
Gud One My blog

Offline

#4 2007-01-28 17:26:27

marios
Archived Plugin Author
Registered: 2005-03-12
Posts: 1,253

Re: Regular Expression needed for reWrite

Try this:

match string:

^([\w\d-]+\b)$

replacement string:

section/$1

I’m not so sure about the htaccess syntax, but the RE does what you need.

regards, marios


⌃ ⇧ < ⌃ ⇧ >

Offline

#5 2007-01-28 17:52:02

hcgtv
Plugin Author
From: Key Largo, Florida
Registered: 2005-11-29
Posts: 2,722
Website

Re: Regular Expression needed for reWrite

zero, You’re right, it creates a never ending loop, just tried it on my local server.

marios, tried your solution, but I get 404’s.

RedirectMatch 301 ^([\w\d-]+\b)$ http://hcg.valleyhaven.net/textpattern/article/$1

Interesting dilemma.

Offline

#6 2007-01-28 18:37:18

zero
Member
From: Lancashire
Registered: 2004-04-19
Posts: 1,470
Website

Re: Regular Expression needed for reWrite

Many thanks, marios, I think your way works!

Within htaccess, straight after RewriteEngine On, I put this:

RewriteRule ^([\w\d-]+\b)$ http://mysite/section/$1 [R=301,L]

I haven’t put a RewriteCond or anything else in there. I need more testing to be sure but it seems to work!


BB6 Band My band
Gud One My blog

Offline

#7 2007-01-28 18:52:45

hcgtv
Plugin Author
From: Key Largo, Florida
Registered: 2005-11-29
Posts: 2,722
Website

Re: Regular Expression needed for reWrite

zero wrote:

Within htaccess, straight after RewriteEngine On, I put this:
RewriteRule ^([\w\d-]+\b)$ http://mysite/section/$1 [R=301,L]

I didn’t have it in the right place, it does work now. Now one caveat, you can’t have a trailing slash like http://mysite/first-post/ but I guess that’s never going to happen.

marios, sorry I doubted you :)

Offline

Board footer

Powered by FluxBB