SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Threaded View

  1. #1
    SitePoint Enthusiast andygout's Avatar
    Join Date
    Jun 2012
    Location
    London, United Kingdom, United Kingdom
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .htaccess RewriteRule clean URL (remove punctuation; replace spaces with hyphens,etc)

    Hello,

    I am trying to create a clean URL using a RewriteRule in an .htaccess file (using Apache 2.2).

    Using a hypothetical example, I would like this:
    Ripley’s Believe It Or Not – Piccadilly Circus (London, England)

    To appear like this:
    attraction/ripleys-believe-it-or-not-piccadilly-circus-london-england

    i.e. Remove all punctuation, replace spaces with hyphens, and make upper case letters lower case. The number of spaces will vary from entry to entry and could be even more than the eight here, so I expect the [N] suffix may well be required.

    I am currently using the ‘id’ (below) rather than the ‘attraction_name’, which is obviously far simpler, but does not create a very useful or attractive URL:
    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule ^attraction/([0-9]*)$ attraction/?id=$1 [L,NC,QSA]
    I have also used a PHP custom function (‘GenerateUrl’) to generate the URL I need from within the link (below), but with this method (found at this site) the variable is not passed to the next page in its original state and therefore cannot then be used to select corresponding data.
    PHP Code:
    <a href = "/attraction/<?php echo GenerateUrl($attraction['id']); ?>"><?php echo html($attraction[attraction_name']); ?></a>
    I don’t want to use the ‘RewriteMap myquery’ method as once my site goes live I don’t expect I’ll have access to the server’s httpd.conf or virtualhost configuration files, which that would require.

    I’ve considered using the custom function to create a URL that can be saved in the ‘attraction’ table and therefore be used to select corresponding data thereafter, but would rather not given I’m pretty sure it’s avoidable.

    I just can’t figure out what the RewriteRule should be – can anybody help me out?

    <snip><merged from hijacked thread><edited>
    DK or ScallioXTX seeing as you each seem to be have expertise on RewriteRules, I hope that you might be able to help.
    </snip></merged></edited>

    Thanks in advance,

    Andy
    Last edited by ServerStorm; Nov 20, 2012 at 16:20.


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
  •