.htaccess file ([^\/]+) Not Work, but ([1-9]+[0-9]*) work? - .htaccess

My project url is /catalog/id/
EG:domain.com/life/women/268/
I had try many times. Why the htaccess can't rewrite the "women", and just rewrite the number '/268/'
Not Work
RewriteRule ^life/([^\/]+)/?$ life/list.php?cat=$1 [L,NC]
RewriteRule ^life/([^\/]+)/([^\/]+)/?$ life/info.php?cat=$1&id=$2 [L,NC]
Not Work result
Array ( [cat] => list.php )
If all use number, Work
RewriteRule ^life/([1-9]+[0-9]*)/?$ life/list.php?cat=$1 [L,NC]
RewriteRule ^life/([1-9]+[0-9]*)/([1-9]+[0-9]*)/?$ life/info.php?cat=$1&id=$2 [L,NC]

Your first RewriteRule rewrites life/list.php and life/info.php. You should exclude them:
RewriteRule ^life/(?!(?:list|info)\.php\/?$)([^\/]+)/?$ life/list.php?cat=$1 [L,NC]
RewriteRule ^life/([^\/]+)/([^\/]+)/?$ life/info.php?cat=$1&id=$2 [L,NC]

Related

.htaccess rewrite category filters

I'm wondering if it's possible to write these lines into one
RewriteRule ^([^/]*)/sd$ /category.php?category_id=$1 [L,NC]
RewriteRule ^([^/]*)/sd/(.*)$ /category.php?category_id=$1&filters=$2 [L,NC]
I've tried with
RewriteRule ^([^/]*)/sd/?(.*)$ /category.php?category_id=$1&filters=$2 [L,NC]
but it doesn't works.
Thanks in advance.

RewriteRule not working for directories with starting and trailing slash

I have an URL like these:
http://domain.com/database/movie/jurassic-park.1224.html
http://domain.com/artist/bruno-mars.104.html
I tried to resolve these URLs with the following RewriteRules, but they don't work.
RewriteEngine On
RewriteRule ^(.*)\/movie\/(.*)\.(.*)\.html$ ./index.php?area=movies&id=$2 [QSA]
RewriteRule ^(.*)\/artist\/(.*)\.(.*)\.html$ ./index.php?area=people&id=$2 [QSA]
Who can help me what's wrong?
Try these rules:
RewriteEngine On
RewriteRule movie/(.+?)\.(\d+)\.html$ index.php?area=movies&id=$2 [L,QSA,NC]
RewriteRule artist/(.+?)\.(\d+)\.html$ index.php?area=people&id=$2 [L,QSA,NC]

GET parameters in htaccess url rewriting

I'm doing some url rewrite with htaccess. I have this:
RewriteRule ^mesas$ http://www.myweb.net/piezas.php?pos=1 [L]
RewriteRule ^sillas$ http://www.myweb.net/piezas.php?pos=2 [L]
RewriteRule ^taburetes$ http://www.myweb.net/piezas.php?pos=3 [L]
This is working. If I write the url http://www.myweb.net/mesas
I get http://www.myweb.net/piezas.php?pos=1.
Now I want to add other parameters. Example:
I have this http://www.myweb.net/mesas?pag=10
and I want this: http://www.myweb.net/piezas.php?pos=1&pag=10
I'm try with:
RewriteRule ^mesas?(.*)$ http://www.myweb.net/piezas.php?pos=1&$1 [L]
and
RewriteRule ^mesas$?(.*) http://www.myweb.net/piezas.php?pos=1&$1 [L]
But it doesn't work. What am I doing wrong?
Thanks!
Try adding flag QSA, like this:
RewriteRule ^mesas$ http://www.myweb.net/piezas.php?pos=1 [L,QSA]
RewriteRule ^sillas$ http://www.myweb.net/piezas.php?pos=2 [L,QSA]
RewriteRule ^taburetes$ http://www.myweb.net/piezas.php?pos=3 [L,QSA]
It will append the incoming query string to the existing one.

RewriteRule same name as directory

For my site I have a directory called /test/. I want to rewrite www.example.com/nl/test and www.example.com/nl/test/ to a certain page (test.php).
Some global conditions (for all the rules)
RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R]
RewriteBase /
RewriteRule ^(nl|en)$ $1/ [NC,R]
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ en/$2 [L,R=302]
RewriteRule ^(nl|en)/(.*)$ $2?language=$1&%{QUERY_STRING} [L]
RewriteRule ^sale$ sale.php
RewriteRule ^valentine$ valentine.php
Some conditions for the rewrite + folder
RewriteRule ^test/$ test.php
The redirect of www.example.com/nl/test/ is correct. The language parameter is also correctly rewritten.
For the second redirect (the version without the trailing slash) I can't get this working.
RewriteRule ^test$ test.php
Now my URL is rewritten as www.example.com/test/?language=nl
Can someone give me a tip or hint to fix this? I can't change the name of the directory since there are several external URLs linking to this directory.
This rule will do the whole job (instead of 4 lines you have there): it will rewrite both /nl/test and /nl/test/ to /test.php?language=nl.
RewriteRule ^(en|nl)/test/?$ /test.php?language=$1 [NC,QSA,L]
NOTES:
The [QSA] flag will preserve any existing query string (therefore, there is no need for &%{QUERY_STRING}).
Full .htaccess:
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ /en/$2 [R=302,L]
RewriteRule ^(nl|en)/(.*)$ /$2?language=$1 [NC,QSA,L]
RewriteRule ^sale/?$ sale.php [QSA,L]
RewriteRule ^valentine/?$ valentine.php [QSA,L]
RewriteRule ^test/?$ test.php [QSA,L]
NOTES:
There is no need for RewriteRule ^(nl|en)$ $1/ [NC,R] as you already have RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]. It does the same job.

RewriteRule - a.php?a=123 to /b/123

I am trying to get Apache to redirect /a.php?a=123 to /b/123 (where 123 could be any number between 1 and 9999) but can't seem to get it to work.
This is what I have in htaccess:
RewriteEngine on
RewriteRule ^a.php?a=([0-9]+) /b/$1 [L]
RewriteRule ^a.php$ /c/ [L]
With this going to a.php?a=123 results in 404, but going to just a.php works as expected.
I tried escaping the ? (RewriteRule ^a.php\?a=([0-9]+) /b/$1 [L]) but it still doesn't work.
What am I doing wrong please?
The query string is not part of the URI path that is tested in the RewriteRule directive. This can only be tested with a RewriteCond directive:
RewriteCond %{QUERY_STRING} ^a=([0-9]+)$
RewriteRule ^a\.php$ /b/%1? [L,R]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^a\.php$ /c/ [L,R]
But if you want it the other way (requests of /b/123 are redirected to /a.php?a=123):
RewriteRule ^b/([0-9]+)$ a.php?a=$1 [L]

Resources