1

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] 
Gautam P Behera
  • 171
  • 2
  • 13

2 Answers2

0

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 ) 
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • tried the above as well. The problem is the value of the page keeps appending when I click on a subgroup link for the second time. array value is al right for 1st click but on second click I am getting 404 Error :-( . Array ( [page] => Shoes [subgroup] => Sports ) – Gautam P Behera Feb 06 '16 at 11:28
  • and how exactly are the urls in your links written? `shoes/tennis` or `/shoes/tennis`? You need to write the links with a leading slash - `/Shoes/tennis` – Professor Abronsius Feb 06 '16 at 11:30
  • http://localhost:8080/series/dynamics/testurl/Shoes/tennis if i am putting a leading slash I am loosing directory detail http://localhost:8080/Shoes/tennis, which is again giving me 404 error – Gautam P Behera Feb 06 '16 at 12:51
  • in your original `Sports
    ` ~ change to `Sports
    `
    – Professor Abronsius Feb 06 '16 at 12:53
  • Ram I am getting your point, when I put a leading slash as per above it takes me to http://localhost:8080/Shoes/Sports and I am getting 404 error. Object not found! The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error. If you think this is a server error, please contact the webmaster. Error 404 localhost Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.14 – Gautam P Behera Feb 06 '16 at 15:31
  • which particular `RewriteBase` are you using? – Professor Abronsius Feb 06 '16 at 15:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102787/discussion-between-gautam-p-behera-and-ramraider). – Gautam P Behera Feb 06 '16 at 15:50
0

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Than you so much @anubhava getting the result, I was after. – Gautam P Behera Feb 07 '16 at 03:05
  • I wd also like to find out how could I be able to remove /series/dynamics/testurl/ from the url to reflect as the web address on the location bar as http://localhost:8080/Shoes/Tennis. Could you be able advise on that. – Gautam P Behera Feb 07 '16 at 03:50
  • I suggest you create a new question for that since that won't be a small change in current answer. – anubhava Feb 07 '16 at 05:30