Camillo
Camillo

Reputation: 544

mod_rewrite for URL with two URL-parameters

I want to be able to open the following url

http://example.com/login/12345

and in the background, it should load:

http://example.com/index.php?endpoint=login&access_key=12345

Obviously, I have tried many htaccess generators to create an appropriate RewriteRule set.

For example the following (from: http://www.generateit.net/mod-rewrite/index.php):

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?endpoint=$1&access_key=$2 [L]

Even though I know for a fact that .htaccess and mod_rewrite is enabled on my server (i tried to setup a test to redirect all requests from example.com to example2.com using htaccess and mod_rewrite, and it worked), I can't get this to work.

I have now spent nearly 2 hours to find for a solution on stackoverflow and other websites, but all my attempts failed.

I hope someone can help me find out the correct way to rewrite my url in the above example.like explained above.

Thanks a lot in advance!

Upvotes: 0

Views: 116

Answers (2)

zx81
zx81

Reputation: 41838

Add this to the .htaccess file in your DOCUMENT_ROOT

Assuming login is not variable

RewriteEngine On
RewriteRule ^login/(\d+)$ index.php?endpoint=login&access_key=$1 [L,NC,DPI]

** If login is variable**

This is in case you want to redirect not only for login, but also for urls such as http://example.com/housenumber/12345

RewriteEngine On
RewriteRule ^([^/]+)/(\d+)$ index.php?endpoint=$1&access_key=$2 [L,NC,DPI]

Tested on actual Apache 2.2 and 2.4 :)

Upvotes: 1

clami219
clami219

Reputation: 3038

Try with this:

RewriteEngine On
RewriteRule ^([^\/]*)/([^\/]*)$ /index.php?endpoint=$1&access_key=$2 [L]
#-Added-these---^--------^

When tested, here is the result:

enter image description here

Upvotes: 2

Related Questions