0

I would like to change

detail.php?cid=$category&ccid=$category2&cccid=$category3&fid=$id&ftitle=$titleseo

to

detail/$category/$category2/$category3/$id/$titleseo

But in case one of the parameters equals 0, the parameter should not be visible. So in case $category=0, the url should become:

detail/$category2/$category3/$id/$titleseo

In case $category2=0, the url should become:

detail/$category/$category3/$id/$titleseo

Is this possible with a rewrite rule in htaccess and if yes how should this look like?

Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
Spiek Vier
  • 65
  • 8

2 Answers2

1

You could use this considering /$category/$category2/$category3/ have given names /$id/$titleseo are fixed :

RewriteEngine On
RewriteBase  /
RewriteRule ^detail/(\$category)?/?(\$category2)?/?(\$category3)?/?(\$id)/(\$titleseo)$  - [E=PASS:cid=$1&ccid=$2&cccid=$3&fid=$4&ftitle=$5]
RewriteCond %{ENV:PASS} (.*)(&)[a-z]+=&(.*)$|[a-z]+=&(.*)$
RewriteRule  ^      details.php?%1%2%3%4 [L]
RewriteCond %{QUERY_STRING} (.*)(&)[a-z]+=&(.*)$|[a-z]+=&(.*)$
RewriteRule  ^      details.php?%1%2%3%4 [L]
RewriteCond %{ENV:PASS} ^(.+)$
RewriteRule  ^      details.php?%1 [L]

In general use this :

RewriteEngine On
RewriteBase /
RewriteRule ^detail/([^/]+)?/?([^/]+)?/?([^/]+)?/?(\$id)/(\$titleseo)$  - [E=PASS:cid=$1&ccid=$2&cccid=$3&fid=$4&ftitle=$5]
RewriteCond %{ENV:PASS} (.*)(&)[a-z]+=&(.*)$|[a-z]+=&(.*)$
RewriteRule  ^      details.php?%1%2%3%4 [L]
RewriteCond %{QUERY_STRING} (.*)(&)[a-z]+=&(.*)$|[a-z]+=&(.*)$
RewriteRule  ^      details.php?%1%2%3%4 [L]
RewriteCond %{ENV:PASS} ^(.+)$
RewriteRule  ^      details.php?%1 [L]

The rules above , first make and environment by capturing any request start with details and ended with /$id/$titleseo then check that ENV values to see which one has/hasn't a value and finally redirect it accordingly.

Both rules work in case of three levels of categories if you need more you could edit the rules like this :

Add this ([^/]+)?/? after RewriteRule ^detail/ then [E=PASS:cid=$1&ccid=$2&cccid=$3&ccccid=$4&fid=$5&ftitle=$6] at the end , you could compare to see the difrenec between this and previous one to get a criteria , and finally add %5 to /details.php?%1%2%3%4 [L] like this /details.php?%1%2%3%4%5 [L] and so on .

Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
  • Many thanks Mohammed, somehow this does not work for me. If I enter detail/schlagzeilen/algemein/0/497/kitas-bahnen-muellabfuhr-hier-wird-ab-dienstag-in-der-region-gestreikt I get error 404. I have currently the following rule that works: RewriteRule ^detail/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) detail.php?cid=$1&ccid=$2&cccid=$3&fid=$4&&ftitle=$5 But like said it leaves the categories that are 0 also in, and i would to leave them out. I hope you can help me. – Spiek Vier Apr 08 '18 at 16:56
  • @SpiekVier put RewriteBase / under RewriteEngine On and test again – Mohammed Elhag Apr 08 '18 at 17:06
0

Did not get the code from Mohammed to work so far, but used

RewriteRule ^detail/?([0-9a-zA-Z]+)?/?([0-9a-zA-Z]+)?/?([0-9a-zA-Z]+)?/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) detail.php?cid=$1&ccid=$2&cccid=$3&fid=$4&&ftitle=$5
Spiek Vier
  • 65
  • 8