Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess mod rewrite $_GET to variables with slashes

I would like to rewrite URL's with htaccess to better readable URL's and use the $_GET variable in PHP
I sometimes make use of a subdomain so it has to work with and without. Also are the variables not necessary in the url. I take a maximum of 3 variables in the URL

the URL sub.mydomain.com/page/a/1/b/2/c/3 should lead to sub.mydomain.com/page.php?a=1&b=2&c=3 and the url sub.mydomain.com/a/1/b/2/c/3 should lead to sub.mydomain.com/index.php?a=1&b=2&c=3 where $_GET['a'] = 1

I came up with this after searching and trying a lot

RewriteEngine on
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4&$5=$6&$7=$8 [QSA,NC]  
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3&$4=$5&$6=$7 [QSA,NC]  
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4&$5=$6 [QSA,NC]  
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3&$4=$5 [QSA,NC]  
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4 [QSA,NC]  
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3 [QSA,NC]  
RewriteRule ([^/]+)\.domain.com/([^/]+)$ $1.domain.com/$2.php [L,QSA,NC]  

but what I get is an not found server error

I'm not that good at this so maybe I oversee something.
Also I would like it to work with and without a slash at the end

Should I make use of RewriteCond and/or set some options?

Thanks in advance.

like image 589
Dediqated Avatar asked Nov 22 '12 13:11

Dediqated


1 Answers

When using RewriteRule, you don't include the domain name in the line. Also, make sure you turn on the RewriteEngine first. Like this:

RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)$  index.php?$1=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$  index.php?$1=$2&$3=$4
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$  index.php?$1=$2&$3=$4&$5=$6

The first line will rewrite sub.mydomain.com/a/1 to sub.mydomain.com/page.php?a=1, the second rewrites sub.mydomain.com/a/1/b/2 to sub.mydomain.com/page.php?a=1&b=2, and so on.

like image 54
ONOZ Avatar answered Sep 25 '22 01:09

ONOZ