Jump to content

Using Hypens Instead of Hypens


justlukeyou

Recommended Posts

Hi,

 

Im puzzled by one issue that could pose a real problem.  I can query my MD using spaces (corner%20%tv%20unit) but I cant query it using hyphens (corner-tv-unit)

 

The problem I have is that space actually appears in the link.  (.com/corner tv unit)

 

Is it possible to replace this with a hyphen or is their a much deeper problem?

Link to comment
Share on other sites

Thanks, Im surprised it allows spaces.  I thought links would feature hyphens as standard.

 

Would the code be in the HT access file.  That's what I'm really struggling with, I can make pretty URLs that look really nice like .com/products/nintendo I just can't put the hyphen in .com/products/nintendo-wii

 

But I can do .com/nintendo wii

 

Its so bloody frustrating!

Link to comment
Share on other sites

Very simple.

 

Assuming you want that match specifically you can do

 

^/?(a-zA-Z0-9-)?/?(a-zA-Z0-9-)?/?$

 

That matches lower and upper case letters, numbers and hyphens. You may also want \+ to include plus signs.

 

Alternatively, just write

 

^(a-zA-Z0-9-\+)$ index.php/$1

 

And it'll map everything matching the criteria.

Link to comment
Share on other sites

Hi,

 

Many thanks, I tried those but couldn't get them to work.  I have tried these also but they appear to work.

 

RewriteRule ^products/extendingdiningtable/([a-zA-Z-]+)$ /products/extendingdiningtable.php?name=$1

 

RewriteRule ^products/extendingdiningtable/([a-zA-Z\-\s]+)$ /products/extendingdiningtable.php?name=$1

 

With this code just enter it as it is, does it all map from the the index file?

 

^(a-zA-Z0-9-\+)$ index.php/$1

Link to comment
Share on other sites

I think you've misunderstood how that would function and how to implement it.

 

If you want the website to redirect all content to one page you would do

 

RewriteRule ^(.*)$ index.php/$1 [L]

 

What are you actually trying to achieve? If you redirect something you must handle it on the page its being redirected too...

Link to comment
Share on other sites

Hi,

 

I'm not looking to redirect to a page, I am trying to insert hyphens into query links.  For example: .com/products/product.php?name=red-widget

 

I can't seem to be able to make red-widget work.

 

But I can make "red" work or "red%20widget" which leaves a space in the link.

 

When I try "red-widget" It blocks the query.  I just see why it wont work.  Ive made about 30 different attempts to make it work.

Link to comment
Share on other sites

Hi,

 

This is the code I am using.  Could be anything to do with the % I am using the code.  There must be something block hyphens.

 

 

<?php
$name = mysql_real_escape_string($_GET['name']);
$query = mysql_query("
    SELECT name, product_id, rrp, discount, image_link
        FROM productdbase p
        INNER JOIN furniture_groups f
            ON
                f.long_name = p.furniture_group
        WHERE
            name LIKE '%{$name}%'
    LIMIT 15");

while ($query_row = mysql_fetch_assoc($query)) { 

?>

 

RewriteRule ^products/computerdesks/([a-zA-Z-\-\s]+)$ /products/computerdesks.php?name=$1

Link to comment
Share on other sites

OK, so did you check what the actual query was that was run and the associated error message?

 

$query = "
    SELECT name, product_id, rrp, discount, image_link
    FROM productdbase p
        INNER JOIN furniture_groups f
        ON
            f.long_name = p.furniture_group
    WHERE
        name LIKE '%{$name}%'
    LIMIT 15");

$result = mysql_query($query) or die ("Query: <br>$query<br>Error:<br>".mysql_error());

while ($query_row = mysql_fetch_assoc($result)) { 

Link to comment
Share on other sites

Hi,

 

Im not totally sure what you mean.  I have echoed the result of the query and it displays fine if I use a single word such as "red".  However, as soon as I introduce a hyphen it blocks the whole query and nothing is echoed onto the page.  There are no error messages, its as if there is no code on the page once I introduce a hyphen.

 

 

Link to comment
Share on other sites

How are you building these links? Do you actually have matching data in your database table? For a specific example, 'corner-tv-unit', what is the data in your two database tables that you expect the query to match? Perhaps for that specific name, your join query doesn't have a matching entry in the furniture_groups table?

 

Edit: I just tried your rewrite rule and your example link and the correct value is received in the computerdesks.php file as the name, so this has nothing to do with that part of the process.

Link to comment
Share on other sites

Im not totally sure what you mean.  I have echoed the result of the query and it displays fine if I use a single word such as "red".  However, as soon as I introduce a hyphen it blocks the whole query and nothing is echoed onto the page.  There are no error messages, its as if there is no code on the page once I introduce a hyphen.

 

Did you even LOOK at the code I provided? There is no such thing as a query being "blocked". Either the query runs or it doesn't. If it doesn't run an error will be generated. So, if the query doesn't run you should check 1) the actual content of the query and 2) the generated error. When a query has any dynamic content, don't assume you know what the query looks like. I provided revised code that added error handling so you can see #1 and #2 above.

Link to comment
Share on other sites

Im not totally sure what you mean.  I have echoed the result of the query and it displays fine if I use a single word such as "red".  However, as soon as I introduce a hyphen it blocks the whole query and nothing is echoed onto the page.  There are no error messages, its as if there is no code on the page once I introduce a hyphen.

 

Did you even LOOK at the code I provided? There is no such thing as a query being "blocked". Either the query runs or it doesn't. If it doesn't run an error will be generated. So, if the query doesn't run you should check 1) the actual content of the query and 2) the generated error. When a query has any dynamic content, don't assume you know what the query looks like. I provided revised code that added error handling so you can see #1 and #2 above.

 

Apologies Psycho, I've had about 2 hours sleep in last 24 hours and didn't notice the changes you made.

 

I have run it, when I use a single word (red) it works fine.  But when I enter a hyphen nothing is echoed.

 

"corner tv unit" is in the database because I can create links with spaces in such as .com/products/corner tv unit  - this works fine.  But .com/products/corner-tv-unit doesn't run.

Link to comment
Share on other sites

No, I thought the HT Access file allowed for the insertion of hyphens.

 

I cant insert hpyhens into the keywords.  Or should I be doing that somehow?

 

Is there a code like the one for spaces (%20) which allows me to insert hyphens.  That would solve the problem!

Link to comment
Share on other sites

Im not totally sure what you mean.  I have echoed the result of the query and it displays fine if I use a single word such as "red".  However, as soon as I introduce a hyphen it blocks the whole query and nothing is echoed onto the page.  There are no error messages, its as if there is no code on the page once I introduce a hyphen.

 

Do you not understand why querying for "tv-stand" will never produce a result when the value in the database is "tv stand"?

 

I think Pikachu has hit the nail on the head. The OP was erroneously suggesting that the query was failing. But, if the code I provided did not produce an error then it must be that he is incorrectly assuming that a hyphen would match a space.  :facewall: All these replies could have been avoided with accurate details provided.

 

@OP: If you want the hyphen in a value to be treated as spaces then do a simple str_replace() on the value before you use it in your query to replace the hyphens with spaced. Alternatively, you could also replace the hyphens with an underscore and change the query to do a LIKE comparison. Then the underscore will be used to match ANY single character.

Link to comment
Share on other sites

Thanks,

 

Apologies but I am new to PHP.  I thought the htaccess code allowed for hyphens as they are so widely used.

 

In terms your suggestions does str_replace() read "corner tv units" and allow me to add a hyphen when the query.  I am completely new to str_replace()

 

To be 100% honest I thought this would be easy as hyphens are so widely used. 

Link to comment
Share on other sites

Thanks,

 

I thought the HT Access would read a link such as .com/products/large-red-widgets and simply ignore the hyphen but the URL bar it showed the hyphen to make a pretty URL.

 

What you have to remember is Im completely new to PHP but I am trying my hardest to use it so when you say "use a simple str_replace()", for me it isn't simple.  The good news is my site is 98% complete.  Just need to do this and finish off pagination.

 

So string replace reads "corner-tv-units" and querys "corner tv units".  If so should I be using this, if so what is $our_url.

 

$our_url = str_replace( " ", "-", <%name%>);

 

Name being the column name I am querying.

 

This seems a very complicated way of doing it.  Much more difficult than using a simple %20. 

Link to comment
Share on other sites

What you have to remember is Im completely new to PHP . . .

 

Your registration date for this site shows February 18, 2007 (6 years ago). But, I'm questioning that date since the earliest post I can find for you is February 02, 2011 - but that is still well over a year ago. That's MORE than enough time to know that there is a manual at php.net and you can easily look up the details of a function with examples and such.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.