1

I have a url like this

fullarticle.php?sID=3&ssID=21&id=1496
sID = category (Forums, entertaiment, news, sports etc)
ssID = sub category (businnes, health, crime, etc)
id = article id

My goal is to make that url look like this

fullarticle/News/Crimes/title

I've tried this at .htaccess:

RewriteRule ^([^/]+)/([^/]+)/([^\.]+)/([^\.]+)/([^\.]+)/([^\.]+)$ fullarticle.php?$1=$2&$3=$4&$5=$6 [L,NC,QSA]

another one

RewriteRule ^/([a-zA-Z\+]+)/([a-zA-Z\+\-]+)/([0-9]+)$ fullarticle.php?sID=3&ssID=21&id=1496

And some other with no luck.

How can I write a rule/condition to accomplish that? If there is an easier way to do this I am open to suggestions.

Thanks for taking the time to look into this.

6
  • 2
    "I have a url like this" ... "to make that url look like this" - to make it look like that you first need to change the URL in your application to make it look like that. Have you already done that? (Apologies, but I have to ask, since so many ask the same question, without doing this first step, expecting to do the whole thing in .htaccess.) Commented Oct 6, 2015 at 16:17
  • no worries. in my php i generate urls like fullarticle.php?sID=3&ssID=21&id=1496. I have tried many RewriteRules trying to make it look like fullarticle/news/crime with no luck. stackoverflow.com/questions/16816342/… mod-rewrite-cheatsheet.com statichtml.com/2010/mod-rewrite-baseon-on-query-string.html Commented Oct 6, 2015 at 16:26
  • (Your PHP should be generating the URLs in the format you want them to look to your users.) How would you intend to convert /news/crime to sID=3&ssID=21 etc.? Where do you get the numeric ID's from? Commented Oct 6, 2015 at 16:33
  • I don't want confrontation but I believe that is what RewriteRule is for. The problem is that all the rules/condition I tried still don't work. Any constructive reply would be appreciated. Commented Oct 6, 2015 at 16:55
  • 1
    that gives me a clue. Probably I will able to answer my own question soon. Commented Oct 6, 2015 at 18:21

2 Answers 2

1

You could try RewriteMap to map from names to numeric ids. But this requires to set up such a map before, e.g.

categories.txt:

news 1
sports 2
entertainement 3

subcategories.txt:

crime 11
baseball 21
football 22
movie 31

titles.txt:

rewrite-rules-solved 83
stackoverflow-rocks 77
you-are-the-greatest 27

and then use that in a RewriteRule

RewriteMap cat "txt:/path/to/categories.txt"
RewriteMap sub "txt:/path/to/subcategories.txt"
RewriteMap title "txt:/path/to/titles.txt"

RewriteRule ^/(.*?)/(.*?)/(.*)$ fullarticle.php?sID=${cat:$1}&ssID=${sub:$2}&id=${title:$3} [L]

Instead of txt files, you can also use prg or dbd mappings. This would allow to retrieve the name to id mapping from a program or an SQL select statement. See the RewriteMap manual for more details.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Olaf. That actually works. However; it is going to be lot of work for maintenance. Our Categories, sub Categories, and title come from the database and keeps on adding/changing. I was thinking into converting the urls to fullarticle/News-3/Crimes-21/title-1496 and get the values from it. I am the process of separating News and 3, Crimes and 21, etc. Thanks again
If you have these already in a database, you could try a prg or dbd mapping instead of a txt file. See updated answer.
Thanks for your time and suggestions Olaf. I appreciate that. However; I needed this working right away. I promise thought that I will look into prg or dbd which are topics out of my league at this point but looks promising for future reference.
0

What I ended up doing was build my dynamic URLs like this:

fullarticle/3/21/1496/title.html

where 3 = category, 21 = sub category and 1496 = title id

My rewrite looks like this:

RewriteRule ^fullarticle/([^/]+)/([^/]+)/([^\.]+)/([a-zA-Z0-9-]+).html$ fullarticle.php?sID=$1&ssID=$2&id=$3 [L,NC,QSA]

I am a novice and it only took me 6 hours to figure it out. I am proud of my self!

1 Comment

You should accept your own answer, so everybody can see the question is resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.