当存在多个参数时,如何仅在干净URL中保留最后一个参数动态

How can I make the last parameter of my url dynamic when I have more then 1 parameters

For instance: http://localhost:8080/series/dynamics/testurl/Shoes/Sports

has 2 parameters namely "shoes" & "Sports". What I would like to achieve is keep Shoes as a static parameter & "Sports" as dynamic parameter which can be changed further to "Running","Tennis" or "Casual". Below is how I have defined the links in php:

<a href="<?php echo 'Shoes'.'/'.$var2; ?>">Sports</a><br/>
<a href="<?php echo 'Shoes'.'/'.$var2; ?>">Running</a><br/>
<a href="<?php echo 'Shoes'.'/'.$var3; ?>">Tennis</a><br/>
<a href="<?php echo 'Shoes'.'/'.$var4; ?>">Casual</a><br/>

While trying to achieve it through above code & htaccess my problem is as soon as an user clicks on Tennis while on "Sports" the url changes to below:

http://localhost:8080/series/dynamics/testurl/Shoes/Shoes/Tennis

Or even if when I click on the sports link for a 2nd time it changes to:

http://localhost:8080/series/dynamics/testurl/Shoes/Shoes/Sports

as a result I am getting 404 Not found error. Hence I am hoping there must be way to keep the 1st parameter staic and only 2nd parameter dynamic. I know its not a very classic way of doing things but I have gon to elementary level in order to be able narrow down on my errors but no luck yet !!!

I checked various sources but nothing was helpful to resolve my problem. Please guide me in the right direction.

.htaccess looks like this:

RewriteEngine On
 RewriteBase /series/dynamics/testurl/
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^([a-zA-Z]+)/?$ test.php?page=$1 [L,NC,QSA]
 RewriteRule ^([a-zA-Z]+)/([^/]+)/?$ test.php?page=$1&subgroup=$2 [L,NC,QSA] 

Looks it is a case where you need a <base> tag in your HTML to handle relative URLs.

You can add this just below <head> section of your page's HTML:

<base href="/series/dynamics/testurl/" />

so that every relative URL is resolved from that base URL and not from the current page's URL.

I think this might be ok

RewriteEngine On
RewriteBase /series/dynamics/testurl/
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)(/?)$ test.php?page=$1&subgroup=$2 [L,NC] 

Ah yes, silly me. If you need to keep /series/dynamics/testurl/ in the url then the rewrite path should be simply / This would mean your urls would be written like this: http://localhost/series/dynamics/testurl/Shoes/Tennis

Otherwise, if you want to have a url like http://localhost/Shoes/Tennis then you would use the full rewrite path as specified originally ~ ie: /series/dynamics/testurl/

So, in summary:

RewriteEngine On
RewriteBase /series/dynamics/testurl/
RewriteRule ^([a-zA-Z0-9\.\-\_]+)/([a-zA-Z0-9\.\-\_]+)(/?)$ test.php?page=$1&subgroup=$2 [L,NC] 
# write your url like this: `http://localhost/Shoes/Tennis`

# Or

RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9\.\-\_]+)/([a-zA-Z0-9\.\-\_]+)(/?)$ test.php?page=$1&subgroup=$2 [L,NC] 
# write your urls like this: `http://localhost/series/dynamics/testurl/Shoes/Tennis`

As a test here I did https://localhost/boots/rock-climbing which, when printing out the $_GET variables, produced:

Array ( [page] => boots [subgroup] => rock-climbing )