2

I am really stuck on a rewrite rule and wonder if anyone can help.

I am pulling data from a database, and as an example the field "name" i use sometimes has 2 words in with a space between. I use this alot on the site but in the url i want to replace the space with a - as i use this field as a variable to determine the record i want to pull.

so i need to turn clubs/premier league/manchester city/

into

clubs/premier-league/manchester-city/

thanks in advance

richard

1 Answer 1

2

Can you try this rule in your .htaccess:

RewriteEngine on RewriteRule ^clubs/([^\s]+)\s([^\s]+)/([^\s]+)\s([^\s]+)/$ /clubs/$1-$2/$3-$4/ [R=302,NC,L] 
Sign up to request clarification or add additional context in comments.

4 Comments

thanks very much for the help. the problem i have is both those names are one variable each, so premier league is one variable, so i dont think that will work my htaccess file at the moment uses: ##RewriteRule ^clubs/(.*)/(.*)/$ teams.php?competition=$1&id=$2 which works with the url clubs/premier league/manchester city/ but not if i replace the spaces with - :(
@richard grantham: I thought the whole purpose of replacing space with hyphen was that you want to use as variable in some code. If that is not the case then I will have to ask what is the purpose of having of having this translation of space to hyphen in rewrite rule?
sorry its quite difficult to explain and im not doing a very good job. basically www.myurl.com/clubs/premier league/manchester city/ looks into a table and returns a league of "premier league" and a team of "manchester city" the problem i have is for seo i want hyphonated urls, but they need to be as they are to pull out the right data from the database, maybe rewrite is the correct approach, can you advice me on the best way to achieve this by anychance? :)
@richard grantham: Thanks, its much clear now. So removing spaces for SEO friendly URL but you still want spaces for your select query. I would suggest keeping rewrite rules as I suggested and inside your code replace hyphens with spaces again just before your SQL query. Don't know which language you are using but all of them have simple string functions to replace one character with another. That way you will have SEO compliant URLs and your queries will also work fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.