1

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!!

Deimos
  • 269
  • 1
  • 6
  • 17

1 Answers1

1

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?

Community
  • 1
  • 1
Dmytro Dzyubak
  • 1,562
  • 2
  • 22
  • 35
  • Its, works, but you know how i can make like wordpress permanlinks? you know, wordpress have the option to display like this www.domain.com/?p=test or www.domain.com/?page_id=1 and show www.domain.com/test how i can do this? – Deimos Mar 31 '14 at 20:55
  • I'm not sure about the Wordpress, but do You mean that you do not need the "index.php" part in URL rewrite? Some PHP page still has to process Your request. – Dmytro Dzyubak Mar 31 '14 at 21:06
  • Yes, but the problem is, i dont want to GET the product by the name, because if you change the name of the product is broken the link, is much better by the ID, and then show the name, in pretty link, is possible to do that? – Deimos Mar 31 '14 at 21:14
  • You can send "33" using method POST, then it won't appear in the URL. Then it will be in the `$_POST` variable (on the PHP part). You won't be able to rewrite the URL after the page has been loaded. – Dmytro Dzyubak Mar 31 '14 at 21:16
  • i update my question with the code of the button, how i'll make the query via post, i can't understand? – Deimos Mar 31 '14 at 21:24
  • EXCELENTS! works perfect with the form, but how i can implement in the htaccess? I just copy this in my htaccess??RewriteEngine On RewriteRule ^([a-z\0-9]+)/([A-Z\a-z\0-9\-\_]+)/$ /index.php?seite=$1&name=$2 [L] – Deimos Mar 31 '14 at 22:47
  • Answer updated with some choices that are possible. In short, it is possible to use combination of PHP, .htaccess, JS, AJAX, but that would be tricky. – Dmytro Dzyubak Mar 31 '14 at 23:30