.htacces RewriteRule problem - .htaccess

This is working:
RewriteRule ^([a-z]{2}/){0,1}showCategory/([0-9]*)/([0-9]*)(/{0,1})$ /main.php?id=$2&il[lang]=$1&page=$3 [L]
with this URL:
http://localhost/showCategory/590/10
Now I want to work with this too:
http://localhost/showCategory/590/transport/10
The rule I tried:
RewriteRule ^([a-z]{2}/){0,1}showCategory/([0-9]*)/([a-z\-_0-9\+]*)/([0-9]*)(/{0,1})$ /main.php?id=$2&il[lang]=$1&page=$3 [L]
How to change the RewriteRule?

This one works with both URLs
RewriteRule ^([a-z]{2}/)?showCategory/([0-9]+)(?:/[A-Za-z0-9_-]+)?/?([0-9]+)/?$ /main.php?id=$2&il[lang]=$1&page=$3 [L]
I also simplified the {0,1} with ? which does the same.
The key is in (?:/[A-Za-z0-9_-]+)?, explanation:
(?: Non-capturing group, so what's in here will not be put in any $n
/ Match the slash
[A-Za-z0-9_-]+ Match any word with letters/numbers/dashes/underscores
)? Close non-capturing group, and the ? means it's optional

Try this rule:
RewriteRule ^([a-z]{2}/)?showCategory/([0-9]+)/[a-z-_0-9+]+/([0-9]+)/?$ /main.php?id=$2&il[lang]=$1&page=$3 [L]

Related

rewriterule to mach "foldername" to "00_foldername"

I have 100 folders each start with a number and underscore like 001_folder1, 002_folder2.
I wrote 100 lines of rewrite code to match 001_folder1 to folder1 and etc.
RewriteRule ^/?mainFolder\/folder1\/$ mainFolder\/001_folder1/index.php [L]
RewriteRule ^/?mainFolder\/folder2\/$ mainFolder\/002_folder2/index.php [L]
.
.
I don't know if its efficient or not, but I need a general rule to match them.
something like:
RewriteRule ^/?mainFolder\/([a-z]*)$ mainFolder\/[(0-9){3}_$1 [NC,L]
Thanks
You can use this single rule to replae your 100 rules:
RewriteRule ^/?mainFolder/folder(1)/$ /mainFolder/00$1_folder$1/index.php [L,NC]

Remove last / from urls using .htaccess

I would like to remove the last '/' from all urls using a 301 .htaccess redirect. How would I do it? I have tried the following, but it only removes from the first level of directories:
RewriteCond %{REQUEST_URI} ^(/[^/]+)/$
RewriteRule . http://www.mysite.net%1 [L,R=301]
For example, it works for www.mysite.net/first/ but does not work for www.mysite.net/first/second/
Change the [^/]+ to just .+. The ^/ means "match everything except for a slash", so "first" matches but "first/second" doesn't. Also, you don't need a condition here.
RewriteRule ^(.*)/$ http://www.mysite.net/$1 [L,R=301]

Mod Rewrite multiple parameters

I have these rules:
RewriteRule ^farbe-([^-]*)/$ ?farbe=$1 [L]
RewriteRule ^marke-([^-]*)/$ ?marke=$1 [L]
So http://playscout.de/hosen/?marke=diesel/ and http://playscout.de/hosen/?farbe=blau/ becomes http://playscout.de/hosen/marke-diesel/ and http://playscout.de/hosen/farbe-blau/
It works, but i need rules for multiple parameters like http://playscout.de/hosen/marke-diesel/farbe-blau/ so i tryed this for farbe:
RewriteRule ^farbe-([^-]*)/$ &farbe=$1 [L]
Because from second delimeter it have to be &
But it doesn't work. Any suggestions?
Put rule in top, to be executed first:
RewriteRule ^marke-([^-]*)/farbe-([^-]*)/$ ?marke=$1&farbe=$2 [L]

HTACCESS issue, regular expression error?

i have a little problem with my .htaccess. Any suggestion?
RewriteRule vendita/([^/]+)/?$ index.php?page=1&ris_pagina=15&tipo=VENDITA&regione=$1&land=$1_vendita [L]
RewriteRule vendita/([^/]+)/pag-([^/]+)/?$ index.php?page=$2&ris_pagina=15&tipo=VENDITA&regione=$1&land=$1_vendita [L]
RewriteRule vendita/([^/]+)/([^/]+)/?$ index.php?page=1&ris_pagina=15&tipo=VENDITA&provincia=$2&land=$2_vendita [L]
RewriteRule vendita/([^/]+)/([^/]+)/pag-([^/]+)/?$ index.php?page=$3&ris_pagina=15&tipo=VENDITA&provincia=$2&land=$2_vendita [L]
RewriteRule ([^/]+)/vendita/?$ index.php?page=1&ris_pagina=15&tipo=VENDITA&comune=$1&land=$1_vendita [L]
RewriteRule ([^/]+)/vendita/pag-([^/]+)/?$ index.php?page=$2&ris_pagina=15&tipo=VENDITA&comune=$1&land=$1_vendita [L]
EXAMPLES
Url request: http://www.mysite.com/vendita/lombardia/bergamo/ ---> NOT WORKING :(
Url request: http://www.mysite.com/milano/vendita/pag-2/ ---> NOT WORKING :(
Replace the regex with ([a-z]+) for letters and ([0-9]+) for numbers it will avoid the collides between different rules.
you seem to have extraneous ? characters at the end of all your regex patterns.
vendita/([^/]+)/([^/]+)/?$
will not match
http://www.mysite.com/vendita/lombardia/bergamo
http://www.mysite.com/vendita/lombardia/bergamo?
http://www.mysite.com/vendita/lombardia/bergamo/
http://www.mysite.com/vendita/lombardia/bergamo/?a=true
it will match
http://www.mysite.com/vendita/lombardia/bergamo/?
try
^vendita/([^/]+)/([^/]+)

Htaccess rule doesnt work

I have got this rule.. which I put as my first rule
RewriteRule ^categories_compare/$ index.php?app_table_comparison=3 [L]
This is the url that I type:
http://apps.com/categories_compare
The problem is that it doesnt do the redirect.. why is that?
This is part of my htaccess:
RewriteRule ^news2/([^/\.]+).(html)$ index.php?news_url_two=$1 [L]
RewriteRule ^news/([^/\.]+).(html)$ index.php?news_url=$1 [L]
RewriteRule ^categories_compare/?$ index.php?app_table_comparison=3 [L]
I want the last rule to work.
Your regular expression is: ^news3/$, so it wants a trailing / at the end but you're only going to news3. Try changing your rule to:
RewriteRule ^news3/?$ index.php?app_table_comparison=3 [L]
The /? means the slash is optional.
RewriteRule categories_compare http://fuckedapps.com/index.php?app_table_comparison=3 [L]
Okay that is the solution without any ^ or / or ^ or $.
Weird thing that it didnt work when I put ^categories_

Resources