reg expression for RewriteRule

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

reg expression for RewriteRule

Post by gurjit »

hi

i am trying to write a RewriteRule in a .htaccess file.

i have several links on a page like:

Code: Select all

<a href="../search/?searchtext=Chatham&searchid=Search&type=8630&q=">Chatam</a>

for search engine optimistation I want to write a RewriteRule such that if i pass through the link

Code: Select all

<a href="../8630/estate-agents/chatham/search-results/chatham-estate-agents.html">Chatam</a>
it goes to the .htaccess file and runs the file in the directory ../search/?searchtext=Chatham&searchid=Search&type=8630

in the above href ../8630/estate-agents/chatham/search-results/chatham-estate-agents.html, I am passing through the relevant values to run the query

I do not have the following file/folder on the server ../8630/estate-agents/chatham/search-results/chatham-estate-agents.html

the question is will this work?

I have tried to write the rule myself but eveytime I click the link ../8630/estate-agents/chatham/search-results/chatham-estate-agents.html I get a 404 error, this is because the file/folder does not exist

below is my attempt:

Code: Select all

 
RewriteEngine on
RewriteRule ^/([0-9]+)/([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]+)/(.*?).html$ /search/index.php?searchtext=$3&searchid=Search&type=$1
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: reg expression for RewriteRule

Post by Benjamin »

Where is the 404 error coming from? Is it coming from the requested page not existing, or is it coming from the page the regex is redirecting to?
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Re: reg expression for RewriteRule

Post by gurjit »

the 404 comes when i click on the link

Code: Select all

 
<a href="../8630/estate-agents/chatham/search-results/chatham-estate-agents.html">Chatam</a>
 
the folder ="../8630/estate-agents/chatham/search-results/ does not exist and the file chatham-estate-agents.html does not exist. I have placed the .htaccess file in the root folder, so i was hoping if this folder and file does not exist the .htaccess file will match the reg expression

Code: Select all

^/([0-9]+)/([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]+)/(.*?).html$
and redirect to

Code: Select all

/search/index.php?searchtext=$3&searchid=Search&type=$1
 
where $3 is the value chatham, $1 is the value 8630

how can i achieve this.

i will have several links where folders will not exist on the server but the values will be passed through the url link. I want the .htaccess file to read the reg expression - once this matches, redirect it to ]/search/index.php?searchtext=$3&searchid=Search&type=$1, taking the values. this folder ../search/index.php does exist.
Post Reply