2

I am working with the following on a football website and when a user clicks an event they get the following URL format:

  • /live/1/event-name.html

Currently the number in the URL represents the ID number of the page, but ideally I'd like to hide this so it returns the following URL:

  • /live/event-name.html

How can I rewrite and hide the page ID number in the URL path completely.

  • 2
    You can't pass something in the URL and hide it. If it's not in the URL then it's not in the URL. You will need something "unique" in the URL in order to lookup the appropriate resource. For instance, is india-pakistan.html unique and indexed in your database? Take the stack exchange URL structure... the ID (eg. 93745 for this page) must be present in the URL for it to "work". – MrWhite May 27 '16 at 19:26
  • 1
    Yep, you could rewrite it as a long as the event-name.html never appears more than once. As pretty as it may look without the ID number, most times it best to leave them alone. – Simon Hayter May 27 '16 at 19:39
  • @w3dk thats a very good solution but there is an issue with this logic i went over with this logic already,event name cannot be unique like i can have two same matches india vs pakistan mutiple times in a a week if i use events name then there will duplication and i cannot differenciate – user3692459 May 28 '16 at 6:33
  • Simon that's the issue it can appear more than once. – user3692459 May 28 '16 at 6:33
  • In that case, if there is nothing else unique in the URL, then what you are proposing is not possible. – MrWhite May 28 '16 at 9:22
1

In your case, use actual event names instead of ID numbers as unique values both in the URL and any database you are running in the back-end of your website.

Your rewrite rule probably (assuming your server is apache) has a line like this:

RewriteRule ^live/([0-9]+)/(.*)\.html$ processevent.php?ID=$1&Name=$2 [L]

Change that line to:

RewriteRule ^live/(.*)\.html$ processevent.php?Name=$1 [L]

Then again, you'll want to replace .* with a stricter regex expression to ensure appropriate characters are in the URL. Maybe something like this will work better for you:

RewriteRule ^live/([A-Za-z0-9\-]+)\.html$ processevent.php?Name=$1 [L]

In your back-end script, access whatever database your script uses and use the event name to seek relevant data required to process the page. You may want to consider making the event name the primary key of the database table.

  • That's a very good solution but there is an issue with this logic I went over with this logic already,event name cannot be unique like I can have two same matches India vs Pakistan multiple times in a a week if i use events name then there will duplication and I cannot differentiate the purpose of using id was to ensure uniqueness.have a look at this URL I want my URL to be something like this. http://example.com/watch/live/hawthorn-hawks-vs-brisbane-lions-live-streaming – user3692459 May 28 '16 at 6:30
  • Maybe include the event date in the URL as well as the name, and if that still isn't unique enough, then include the estimated time of the event as well. – Mike May 29 '16 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.