Htaccess - how can rewrite my dynamic url's?

I have website on which I would like to rewrite the dynamic urls with htaccess.

Here is example of what I am trying to achieve:

Existing url:

http://www.mysite.com/articles.php?artid=89

Desired url:

http://www.mysite.com/articles.php/add-your-company

Existing url:

http://www.mysite.com/halls.php?fairid=64

Desired url:

http://www.mysite.com/halls.php/building-and-construction

Note that in the above url the word building-and-construction in the database is stored as building and construction.

Anyone can help me with this problem?

Regards, Zoran

Hi,

I think it helps you http://www.iwebtool.com/htaccess_url_rewrite

Regards
InstaPage

Hi

it gives me a result:

Your new URL would be: -artid-40 What I need is my new url to be add-your-company, not -artid-40…

which is not of great help.

Any other solution??

Regards, Zoran

You will either need to keep the id in the name of the url, such as /add-your-company-89 or you will need to be able to make coding changes to your system. As you would have to hit your database to determine /add-your-company is to be served the same information as artid=89.

The problem lies in the fact that your scripts today require an id. When given add-your-company, there isn’t an id provided so your scripts as they exist today won’t be able to serve the page being asked for.

Since i didn’t manage to solve my problem…i will try to reformulate it again:

I have the following url structure:

www.mysite.com/temporary/articles.php?artid=1

I would like to change it with

www.mysite.com/temporary/articles/article-title-here.

Anyone can tell me how can I do that?

Also I have links with two varialbes in it:

www.mysite.com/temporary/products.php?catid=1&productid=1

Which I am trying to make it:

www.mysite.com/temporary/products.php/category-name/product-name

I wouldn’t mind to keep my id in the url…

Thanks in advance.

I still think you are going to need to put the article/category/product id in the URLs. Unless you want to make scripting changes to your website. As htaccess cannot figure out the category/article/product ids. What it can do, is extract them from a url like www.mysite.com/temporary/article-id-article-title-here/ and www.mysite.com/temporary/category-id-category-name/product-id-product-name/

Do either of those options interest you?

Zoran,

Please read the sticky posts OR the link in my signature as the redirections are almost trivial - IF UNIQUE. View http://wilderness-wally.com for an example of how it’s done. If you still have questions (like the acceptable character set), come on back and ask.

Regards,

DK

Hi cpradio

I have no problem to keep an id within my url. Can you point me to some solution? I will soon end with bumping my head against the wall with this problem.

Regards,Zoran

Yes, now that I know that I can use the id in the URL, I’ll work on a solution.

Okay, here it is:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?:.*?)\\/?([0-9]+)\\-[\\d\\w\\s\\-]+\\/([0-9]+)\\-[\\d\\w\\s\\-]+\\/? products.php?catid=$1&productid=$2 [NC,L]
RewriteRule (?:.*?)\\/?([0-9]+)\\-[\\d\\w\\s\\-]+\\/? articles.php?artid=$1 [NC,L]

The first three RewriteCond mean (if there is not a file with the name, a symbolic link with the name or a directory with the name in the URL, then continue on).
The next line handles products, as it expects two sections.
mydomain.com/temporary/##-category-name/##-product-name/ (acceptable, where ## is the category/product id respectively)
mydomain.com/temporary/##-category-name/##-product-name (acceptable, where ## is the category/product id respectively)
mydomain.com/##-category-name/##-product-name/ (acceptable, where ## is the category/product id respectively)
mydomain.com/##-category-name/##-product-name (acceptable, where ## is the category/product id respectively)

The last line handles articles (only expects 1 section)
mydomain.com/temporary/##-article-name (acceptable, where ## is the article id)
mydomain.com/temporary/##-article-name (acceptable, where ## is the article id)
mydomain.com/##-article-name (acceptable, where ## is the article id)
mydomain.com/##-article-name (acceptable, where ## is the article id)

If you ever want to do categories in the same fashion as articles (example: mydomain.com/temporary/42-category-name/), then we need to tweak the rules, as both would expect to only receive one section and can’t be differentiated accordingly (example: mydomain.com/temporary/42-article-name/ looks the same as mydomain/com/temporary/42-category-name/ – we would need to make them look different).

@cpradio; that looks good! One thing though, RewriteCond’s only apply to the next RewriteRule following it. And since the RewriteCond’s should apply to both rules you need to repeat them for the second rule, like so:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?:.*?)\\/?([0-9]+)\\-[\\d\\w\\s\\-]+\\/([0-9]+)\\-[\\d\\w\\s\\-]+\\/? products.php?catid=$1&productid=$2 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?:.*?)\\/?([0-9]+)\\-[\\d\\w\\s\\-]+\\/? articles.php?artid=$1 [NC,L]

:slight_smile:

Ah, yup, definitely missed that. Thanks.

Hi

First of all thanks to all of you who spent some of your time to answer my question. Especially to the people who write this code for me. Now…
I guess that many of you will rolling on the floor from laughing while reading my question…but…how on earth my htaccess file will know what is my article name when the id is for example 89??? Ok, I have the .htaccess file, should I just upload it on the server and it will work???

Regards, Zoran

Zoran,

NO ROFL around here! Members only ask questions when they’re in need of help.

You are providing the catid, productid and artid via the query strings you’re appending. mod_rewrite has been instructed to ignore the name/title/description/whatever the other atom contains but that will be available to article.php via the db query.

FYI, except for the symbolic link test, your code looks much like WordPress’s mod_rewrite code. Within WP’s admin, there is a switch which tells WP to use either the id of the record or the title (for “pretty URIs”). It’s a nice feature that WP added at least a year ago.

Regards,

DK

Not sure exactly if this is the answer you are seeking, but you will still need to update your URLs in your website to use a “pretty” url (.htaccess can’t do that part for you), so where you have URLs like articles.php?artid=89, you will replace that link with /89-article-title-here/

As dklynn, stated, the rewrite rule specifically looks for the number in your URL ( example: /89-article-title-here/; anything not bold is ignored ), it then takes that 89, and tells apache, the request really wants what is stored at articles.php?artid=89 (notice the 89 from your pretty article gets put as the value of artid).

Products works similarly, the only difference is it looks for 2 numbers (a product id and a category id) instead of just one number.

Hi

Once again, thanks to all of you that find time to answer my questions.

Can you give me an example how can I do that? Should I create separate php file which will do this job? Can you write couple lines of code as an example?

Regards,Zoran

Are you using any kind of CMS or software? Can you provide a link to the website? Without seeing it, I really can’t offer a way to update your existing links into the “pretty” links.