SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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/buil...d-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

  2. #2
    SitePoint Member instapage's Avatar
    Join Date
    May 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

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

    Regards
    InstaPage

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    http://www.mysite.com/temporary/arti...cle-title-here.

    Anyone can tell me how can I do that?

    Also I have links with two varialbes in it:

    http://www.mysite.com/temporary/prod...=1&productid=1

    Which I am trying to make it:

    http://www.mysite.com/temporary/prod...e/product-name

    I wouldn't mind to keep my id in the url...

    Thanks in advance.

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    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 http://www.mysite.com/temporary/arti...le-title-here/ and http://www.mysite.com/temporary/cate...-product-name/

    Do either of those options interest you?

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Yes, now that I know that I can use the id in the URL, I'll work on a solution.

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zoreli View Post
    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
    Okay, here it is:
    Code:
    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).

  11. #11
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    @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:

    Code:
    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]
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  12. #12
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    @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:

    Code:
    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]
    Ah, yup, definitely missed that. Thanks.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  14. #14
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  15. #15
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zoreli View Post
    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
    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.

  16. #16
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

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

    Quote Originally Posted by cpradio View Post
    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/
    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

  17. #17
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •