Deimos
Deimos

Reputation: 269

Make Pretty links for this kind of URL

Hi guys how you recommend me to do this url in pretty links?? Im trying with htaccess but i don't find a good solution...

domain.com/?seite=page1&?id=33&name=1-Free-Cocktails!

for example:

www.domain.com/page1/1-Free-Cocktails!/

I have this code

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule    ^page1/([0-9]+)/?$   /?seite=page=$1    [NC,L]    # Handle product requests
RewriteRule    ^page1/  /?seite=page[NC,L]    # Handle product requests

OUTPUT

www.domain.com/page1/

THE BUTTON

    <div class="pull-right kaufen">
<a  href="/?seite=page1&amp;id=<?php echo $item['id'];?>&amp;name=<?php echo $product->prettyLinks($name);?>" class="btn btn-success">Info Anzeigen!</a></div>

I will appreaciate your help!!

Upvotes: 1

Views: 1044

Answers (1)

Dmytro Dzyubak
Dmytro Dzyubak

Reputation: 1642

If You want domain.com/page1/33/1-Free-Cocktails!/ be transformed into http://domain.com/index.php?seite=page1&?id=33&name=1-Free-Cocktails!

Put .htaccess file to domain.com/.htaccess

The contents of .htaccess file should be:

RewriteEngine On
RewriteRule ^([a-z\0-9]+)/([0-9]+)/([A-Z\a-z\0-9\-\_]+)/$ /index.php?seite=$1&?id=$2&name=$3 [L]

If You do not need 33/. That is http://domain.com/page1/1-Free-Cocktails!/ transforms to http://domain.com/index.php?seite=page1&name=1-Free-Cocktails!, then .htaccess would be as follows:

RewriteEngine On
RewriteRule ^([a-z\0-9]+)/([A-Z\a-z\0-9\-\_]+)/$ /index.php?seite=$1&name=$2 [L]

Update: BTW, if You do not want 33/ to appear in the URL You can send it using POST as opposed to using GET.

To use method post You have to use forms. This example has a mix of POST and GET methods. seite and name are sent using the GET method, while id is sent using the POST method. It looks a little bit awkward, but here it is:

<div class="pull-right kaufen">
<form action="index.php?seite=page1&name=<?php echo $product->prettyLinks($name);?>" method="post">
<input type="hidden" value="<?php echo $item['id'];?>" name="id" />
<input type="submit" value="Submit">
</form>
</div>

Then on the PHP page that receives the data, Your id, etc. can be output as follows:

<?php
echo htmlspecialchars($_POST["id"]);
echo htmlspecialchars($_GET["seite"]);
echo htmlspecialchars($_GET["name"]);
?>

Reference:

Apache mod_rewrite Introduction (see "Figure 1" in "Regex Back-Reference Availability" section)

$_POST

Update 2: You won't be able to implement that in .htaccess, because as I stated in one of my comments "You won't be able to rewrite the URL after the page has been loaded". .htaccess is not PHP. Your URL would be as follows http://domain.com/page1/1-Free-Cocktails!/. But You can, for example, map Your "33" id to some predefined name and display it in the <title><?php echo $_POST["id"];?></title> tag.

However, one of the other options that You have is to use JavaScript and AJAX. But that would be another question. You can start Your research here:

Javascript: How do I change the URL in the address bar without refreshing the page?

Is there a way to change the browser's address bar without refreshing the page?

Upvotes: 1

Related Questions