Hi,
I am trying to create a RewriteRule for my site. I got the first part correctly. This means say /index.php?mid=#. This passes correctly.
Here is the example: דף_הבית

But how do I add another param?
Say I am passing /index.php?mid=#&smid=# ????

Here is my rewriterule in htaccess:

Options +FollowSymlinks
RewriteEngine On
#RewriteCond %{REQUEST_URI} !img/(.*)$
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

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

Here is what i do with the params:

//preparing query chunks 
$url = $_GET['q'];
$url = str_replace('_', ' ', $url);
$urlChunksInitial = explode('/', $url);
$urlChunks = array_reverse($urlChunksInitial);


//processing mid chunk 
if (isset($urlChunks[0]) && ($urlChunks[0] != '')) {

	$midTitle = $urlChunks[0];
	$midQuery = mysql_query('SELECT menu_id, title FROM menu WHERE title = \'' . $midTitle . '\'') 
		or trigger_error('Query: ' . $midQuery . '\n<br />MySQL Error: ' . mysql_error());
	if (mysql_num_rows($midQuery) > 0) {
		$midRows = @mysql_fetch_array($midQuery);
		$mid = $midRows[0];
		$menuTitle = $midRows[1];
		$menuUrl = '/'.$midRows[1];
	}

} // if ($urlChunks[0] != '')

// processing smid chunk 
if (isset($urlChunks[1]) && ($urlChunks[1] != '')) {

	$smidTitle = $urlChunks[1];
	$smidQuery = mysql_query('SELECT smenu_id, title FROM smenu WHERE title = \'' . $smidTitle . '\'') 
		or trigger_error('Query: ' . $smidQuery . '\n<br />MySQL Error: ' . mysql_error());
	if (mysql_num_rows($smidQuery) > 0) {
		$smidRows = @mysql_fetch_array($smidQuery);
		$smid = $smidRows[0];
		$smenuTitle = $smidRows[1];
		$smenuUrl = '/'.$smidRows[1];
	}

} // if ($urlChunks[1] != '')

Please help!!! :)

Dani AI

Generated

Short summary and a working, minimal approach.

As was headed in the right direction, the real issues here are (a) using a regex that allows empty matches, (b) rule ordering, and (c) the fact that your PHP expects a single q value and then reverses the segments with array_reverse. To avoid changing the PHP you already have, pass the segments into q in the order your code expects (the last URL segment first).

Try these two rules (put the two-segment rule first):

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

Why this helps

  • [^/]+ forces at least one character (no empty matches); + is preferred over *.
  • Two-segment rule goes first so it matches /a/b before the single-segment rule.
  • QSA preserves any existing query string; L stops further rewriting.
  • In per-directory .htaccess patterns must not start with a leading slash.

Troubleshooting (if you still see 500)

  • Look at Apache error log (common places: /var/log/apache2/error.log or /var/log/httpd/error_log) for the exact message.
  • Hosts sometimes disallow Options +FollowSymLinks; remove or replace it if the error points there.
  • Make sure you did not copy HTML-escaped &amp; into .htaccess (use & in query strings).
  • A rewrite loop can cause a 500 — if needed add a guard like RewriteCond %{REQUEST_URI} !^/index.php$ before the rules.
  • For debugging temporarily add [R=302,L] to a rule to see what URL it produces.

If you prefer to keep your current PHP untouched, use the rules above; if you want separate GET vars instead, map to index.php?mid=$1&smid=$2 and update the PHP accordingly.

Recommended Answers

All 6 Replies

Hi,
I am trying to create a RewriteRule for my site. I got the first part correctly. This means say /index.php?mid=#. This passes correctly.
Here is the example: דף_הבית

But how do I add another param?
Say I am passing /index.php?mid=#&smid=# ????

Here is my rewriterule in htaccess:

Options +FollowSymlinks
RewriteEngine On
#RewriteCond %{REQUEST_URI} !img/(.*)$
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

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

<snip>

Please help!!! :)

Hello mate.

Here is how your .htaccess should look to add the extra functionality you want:

Options +FollowSymlinks
RewriteEngine On
#RewriteCond %{REQUEST_URI} !img/(.*)$
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.+)/^(.+)$ /index.php?q=$1&q2=$2 [NC] 
RewriteRule ^(.+)$ /index.php?q=$1 [NC]

you will now be able to access the second variable in your script as q2 if theres 2 matches, but if theres not 2 matches it will go to the next lin in the htaccess and look for 1 match

when i add this line

RewriteRule ^(.+)/^(.+)$ /index.php?q=$1&q2=$2 [NC]

i get Internal Server Error.
any ideas?

when i add this line
i get Internal Server Error.
any ideas?

hey yeh

try it without the additional ^ that i forgot to remove :-)

soo.....

RewriteRule ^(.+)/(.+)$ /index.php?q=$1&q2=$2 [NC]

i made the change, but i still get internal server error.

i made the change, but i still get internal server error.

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^en/([a-zA-Z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/ index.php?uid=$1&step=$2&function=$3&v1=$4&v2=$5&v3=$6&v4=$7 [NC]

RewriteRule ^en/([a-zA-Z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/ index.php?uid=$1&step=$2&function=$3&v1=$4&v2=$5&v3=$6 [NC]

RewriteRule ^en/([a-zA-Z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/ index.php?uid=$1&step=$2&function=$3&v1=$4&v2=$5 [NC]

RewriteRule ^en/([a-zA-Z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/ index.php?uid=$1&step=$2&function=$3&v1=$4 [NC]

RewriteRule ^en/([a-zA-Z0-9_]*)/([a-z0-9_]*)/([a-z0-9_]*)/ index.php?uid=$1&step=$2&function=$3 [NC]

RewriteRule ^en/([a-zA-Z0-9_]*)/([a-z0-9_]*)/ index.php?uid=$1&step=$2 [NC]

thats a working htaccess on my server.... i don't have time at the minute to test your problem but maybe my htaccess can point you in the right direction. hope this helps! :-)

thanks for the code. im not sure why its not working

RewriteRule ^([a-zA-Z0-9_]*)/([a-z0-9_]*) /index.php?q=$1&step=$2 [NC]
RewriteRule ^([a-zA-Z0-9_]*) /index.php?q=$1 [NC]

The last line works, but the first doesn't.

i dont get it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.