htaccess mod_rewrite multiple paths to query string - .htaccess

I've been searching but I can't quite find what I'm after.
I'm trying to find an elegant way to redirect my url path to the query string via htaccess and mod_rewrite.
The problem is that the path does not have a fixed amound of sub dirs and could be huge.
i.e. http://example.com/sub1/sub2/sub3/sub4/sub5/sub6/sub7...
Currently I just have a load of rules in my htaccess to capture these...
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/? /index.php?sub[]=$1&sub[]=$2&sub[]=$3&sub[]=$4&sub[]=$5 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/? /index.php?sub[]=$1&sub[]=$2&sub[]=$3&sub[]=$4 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? /index.php?sub[]=$1&sub[]=$2&sub[]=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/? /index.php?sub[]=$1&sub[]=$2 [QSA,L]
RewriteRule ^([^/]+)/? /index.php?sub[]=$1 [QSA,L]
... but there may be more, anyone know of a way to autoamtically parse these in to the query string?

If the paramater name is always the same, you could just rely on the rewrite engine to loop through them all:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?sub[]=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.+)$ /$2?sub[]=$1 [L,QSA]

Related

Rewrite multiple url's with htaccess

I'm struggling with this problem a few weeks now. In Google Search Console I get many crawl errors with the same problem: Google cannot find url's that don't even exist.
I've looked in the html-code, but the relative url's are all fine. And I'm using the /-base for all my internal links. I think the problem is my .htaccess file.
On my website nationsleaguevoetbal.nl I have two url's with different rewrites:
/nieuws/item
/wedstrijd/id/land
'land' isn't used and is only for looking nice. Now Google Search Console can't find for example:
/wedstrijd/id/nieuws/item
It combines the two url's where it shouldn't.
My .htaccess rewrite looks like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?pagina=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^nieuws/([^/]+)$ /index.php?pagina=nieuws&item=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wedstrijd/([^/]+)/([^/]+)$ /index.php pagina=wedstrijd&id=$1&landen=$2 [QSA,L]
I thought the QSA would solve the problem, but the errors are coming back. Can you help me please?
Have it this way:
RewriteEngine On
# skip all files and directories from rewrites
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^nieuws/([^/]+)/?$ index.php?pagina=nieuws&item=$1 [QSA,L,NC]
RewriteRule ^wedstrijd/([^/]+)/([^/]+)/?$ index.php?pagina=wedstrijd&id=$1&landen=$2 [QSA,L,NC]
RewriteRule ^(.+?)/?$ index.php?pagina=$1 [QSA,L]

Sub directory as GET with htaccess

I want to use "fake" sub-directories as GET requests. Sorry for my inaccurate wording.
I current have this in my htaccess:
RewriteEngine on
RewriteRule ^pggr/(.*)$ index.php?p=$1
So http://example.com/pggr/blah is treated as http://example.com/?p=blah.
However I want to remove the pggr part from the url, so it would be just http://example.com/blah
You can use this rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) index.php?p=$1 [L,QSA]

I write the right subject but the site don't accept it

How can i rewrite this in .htaccess correctly?
So I have a query string in my url:
/index.php?page=pageCount&subject=mySubject
I want to change it to:
/index/pageCount/mySubject
and also
/show.php?subject=mySubject
to
/mySubject
and also
search.php?key=myKey
to
/key/myKey
thanks
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index/([^/]+)/([^/]+)$ index.php?page=$1&subject=$2 [L,QSA]
RewriteRule ^key/([^/]+)$ search.php?key=$1 [L,QSA]
RewriteRule ^([^/]+)$ show.php?subject=$1 [L,QSA]

rewrite url to root directory with url cutting

I have such urls:
http://site.ru/ontent/wefwefw/article.php?article=369-tayskaya-kuhnya-recept-s-foto
http://site.ru/ontent/wefwefw/article.php?article=32237-ogurci-recepti-na-zimu
http://site.ru/ontent/wefwefw/article.php?article=90-ogurci-na-zimu-recepti-po-koreyski
I want to rewrite tham like:
http://site.ru/tayskaya-kuhnya-recept-s-foto.html
http://site.ru/ogurci-recepti-na-zimu.html
I tried smth like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ontent/wefwefw/$1 [L]
But how cut unnecessary parts of string?
Unless article.php is able to derive the right article from the title alone, what you want to do is not possible. mod_rewrite is good at rewriting things, but it can't summon an article-id from thin air if it isn't in the original request. You would have something like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)\.html$ ontent/wefwefw/article.php?article=$1 [L]
When you would request http://site.ru/tayskaya-kuhnya-recept-s-foto.html, it will load http://site.ru/ontent/wefwefw/article.php?article=tayskaya-kuhnya-recept-s-foto. Then you have to get the id 369 in some other way based on the title if needed.
The best in this case is:
http://site.ru/369-tayskaya-kuhnya-recept-s-foto.html
redirect to
http://site.ru/ontent/wefwefw/article.php?article=369
with:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\d+)-[^/]*\.html$ ontent/wefwefw/article.php?article=$1 [L]

Redirect url get variables after rewriting htaccess

My original url was this:
wwww.domain.com/car-details.php?merk=BMW&model=X5&titel=sale&car_id=3
I have Rewrite it into this:
wwww.domain.com/BMW/X5/sale/3
By putting this in my .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)/([0-9]+)?$ /car-details.php?merk=$1&model=$2&titel=$3&car_id=$4 [L,QSA]
When i put in Url wwww.domain.com/car-details.php?merk=BMW&model=X5&titel=sale&car_id=3 it still works. I want that url to be redirected to wwww.domain.com/BMW/X5/sale/3
I have tried to put the [R] flag in my htaccess RewriteRule but then I'm getting the opposite results.
How can I fix this?
This is a good decision to do what you want.
But you will face a loop problem.
You can avoid it by using this code
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/car-details\.php\?merk=([^&]+)&model=([^&]+)&titel=([^&]+)&car_id=([0-9]+) [NC]
RewriteRule . /%1/%2/%3/%4? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([0-9]+)?$ /car-details.php?merk=$1&model=$2&titel=$3&car_id=$4 [L]

Resources