kur sava
kur sava

Reputation: 43

Special characters in GET parameter with htaccess rules

My rules are generated with free online tools. They work fine until some of special characters like & / - shows up in the url as GET parameter. I want to get clean and SEO friendly url.

I found similar questions, I tried to solve my problem using regex and flags as people answered but it still does not work.

How to handle special characters like & and / in .htaccess rules?

GET parameter doesn't work with htaccess rules

...and many other answers.

My htaccess

RewriteEngine On
RewriteRule ^all-books$ /bookIndex.php [L]
RewriteRule ^year/([^/]*)$ /bookIndex.php?year=$1 [L]
RewriteRule ^find-by-first-letter/([^/]*)$ /bookIndex.php?letter=$1 [L]
RewriteRule ^books-online/([^/]*)/about-book$ /book.php?book=$1 [L]

This is how I am using it.

Pages and codes:

page: https://example.com/bookIndex.php 
url / a href: https://example.com/all-books
Rule: `RewriteRule ^all-books$ /bookIndex.php [L]`

same page with GET params

url: https://example.com/year/[YEAR HERE]
link: echo "<a href=\"https://example.com/year/".$row["year"]."\">".$row["year"]."</a>";
Rule: RewriteRule ^year/([^/]*)$ /bookIndex.php?year=$1 [L]

url: https://example.com/find-by-first-letter/[FIRST LETTER HERE]
link: echo "<a href=\"https://example.com/find-by-first-letter/".$row1["letter"]."\">".$row1["letter"]."</a>";
Rule: RewriteRule ^find-by-first-letter/([^/]*)$ /bookIndex.php?letter=$1 [L]

Regex is fine there since GET param might contain only first letter or numberes. I am not sure about flag, i guess flag is fine.

Page and codes where problem is

page: https://example.com/book.php 
url: https://example.com/books-online/[TITLE HERE]/about-book
link: <a href=\"https://example.com/books-online/".str_replace(' ', '-', $row2['title'])."/about-book\">".$row2['title']."</a>
RewriteRule ^books-online/([^/]*)/about-book$ /book.php?book=$1 [L]

If $row2['title'] = "The Book"` all works fine but in following cases

$row2['title'] = "K-9000"
$row2['title'] = "24/7"
$row2['title'] = "You & Me"

I am getting "The page isn’t redirecting properly" error in browser. I have no clue why rexeg from other answers does not work, do I need RewriteCond %{QUERY_STRING} ?

And what about my way of changing spaces in url into "-" with str_replace(' ', '-', $row2['title'])> ?

I am sure now it was wrong way because i have to change spaces back to get original title to search database while title could be "K-9000", i wont get any result.

Should I use htaccess for that as well, right?

Upvotes: 0

Views: 278

Answers (1)

Kerkouch
Kerkouch

Reputation: 1456

The sanitization str_replace(' ', '-', ...) you used is not enough, you should use another advanced sanization. I created the following function to sanitize the title, it will replace all non alphanumeric charaters with dashes:

/**
 * Replace all not non-alphanumeric characters with dashes
 *
 * @var string $title
 * @return string
 */
function sanitize_title(string $title) {
    $title = strtolower($title);
    $title = str_replace('&', 'and', $title);
    $title = preg_replace('/([^a-z0-9]+)/', '-', $title);
    $title = trim($title, '-');
    return $title;
}

Then when you print the link:

<a href="https://example.com/books-online/" . sanitize_title($row2['title']) . "/about-book">".$row2['title']."</a>

Examples:

One Hundred Years of Solitude: one-hundred-years-of-solitude
       Breakfast at Tiffany's: breakfast-at-tiffany-s
                     Catch-22: catch-22
             Carry On, Jeeves: carry-on-jeeves
            Man & His Symbols: man-and-his-symbols
                  I, Claudius: i-claudius

Upvotes: 1

Related Questions