Mod_rewrite: is it possible to link to the backlink twice?

I have a rewrite rule (in the apache htaccess file) that tries to use the backlink twice from the same capture ($ 1):

RewriteRule ^([A-Za-z0-9_-]+)/?$ $1.php?nav=$1 

It looks like the query string remains emtpy like

 example.com/new 

corresponded as

 example.com/new.php?nav= 

I want to

 example.com/new.php?nav=new 

My question is: can I refer to $ 1 twice in an expression?

UPDATE:

The Apache documentation on mod_rewrite indicates that you can reference the capture as many times as you want in the replacement part of the rewrite rule. However, after trying a couple of days, I could not get it to work. I got my rule to go to the online regular expression testers, which are, but not on my site. I ended up redesigning my menu system to use simpler rewrite rules.

+4
source share
2 answers

This is a regular expression that you use incorrectly:

 ^(A-Za-z0-9-_)$ 

The range is allowed only in square brackets, and you need to use + accessor to match more than 1 character.

Replace the RewriteRule with the following:

 RewriteRule ^([a-z0-9_-]+)/?$ $1.php?nav=$1 [L,NC,QSA] 
+1
source

Try this instead:

 RewriteRule ^([\w]*)/?$ $1.php?nav=$1 [L] 

And yes. Backlinks can be used more than once.

UPDATE

According to your comment:

 RewriteEngine On RewriteBase /information/ RewriteRule ^article([0-9]+)/?$ get-article.php?article=$1 [L] RewriteRule ^blog([0-9]+)/?$ get-blog.php?nav=blog&entry=$1 [L] RewriteCond %{REQUEST_URI} /new RewriteRule ^([\w]*)/?$ $1.php?nav=$1 [L] 

The last condition and the last rule work fine. Result:

http://example.com/new.php?nav=new

0
source

All Articles