Ruf1
Ruf1

Reputation: 179

How can I Create ID from URL using Modrewrite?

In my .htaccess file I have the following code:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]
</IfModule>

In my index.php I have the following code:

<?php
$id_1 = isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : 'default';
?>

Currently, if I use a URL in the following format

http://mydomain.com/directory/name/

The modrewrite grabs /name/ as $id_1

I would like to change the format of the url to grab a second $id_2 as follows

http://mydomain.com/directory/1/name/

my question is how do I change the modrewrite and the php code to capture both $id_1 and $id_2

Upvotes: 1

Views: 61

Answers (1)

CaSUaL
CaSUaL

Reputation: 154

RewriteRule ^([^/]+)/?$ index.php?id_1=$1 [L,QSA]

RewriteRule ^([^/]+)/([^/]+)/?$ index.php?id_1=$1&id_2=$2 [L,QSA]

the QSA means query_string_add so if you call for

http://mydomain.com/directory/1/name/?foo=bar the rewrite engine will translate to http://mydomain.com/directory/?id_1=1&id_2=name&foo=bar

Upvotes: 1

Related Questions