HTACCESS issue, regular expression error? - .htaccess

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/([^/]+)/([^/]+)

Related

.htaccess RewriteRule duplicate Query string

I found several topics related to my problem on stackoverflow.com, but I have not found the solution yet.
This is my .htaccess code :
RewriteEngine On
RewriteRule ^library-(.*)\.aspx$ index.php?page=library&image=$1 [NC,L,QSA]
RewriteRule ^information-(.*)\.aspx$ index.php?page=information&text=$1[NC,L,QSA]
Great, the new formatting of URLs works :
https://example.com/library-image5000.aspx
or
https://example.com/information-sometext.aspx
But, can anyone tell me why the url accept the both of query strings?
https://example.com/library-image5000.aspx/information-sometext.aspx
This is because your regex pattern ^library-(.*)\.aspx$ also matches the URI /library-image5000.aspx/information-sometext.aspx .
You can use use a restricted pattern instead of the catch-all (.*) . Use ([^/]+)as it matches any characters except / .
RewriteEngine On
RewriteRule ^library-([^/]+)\.aspx$ index.php?page=library&image=$1 [NC,L,QSA]
RewriteRule ^information-([^/]+)\.aspx$ index.php?page=information&text=$1[NC,L,QSA]

Weird rewriterule behaviour

I'm hoping someone can help me here. I'm currently trying to make my site more SEO friendly and having some strange issues with .htaccess.
This is all on my testing server. The designer section was working fine without "homeware-designer/" preceding the designer name "cquoi, etc". All good until I added the last one mage then it all stopped working, relative links were no longer working properly. I removed RewriteRule mage designerdata.php?search=4 and it all started working again. Once I added homeware-designer/etc all works fine again???? Can anyone tell me what might be going on. Also some of the rules won't work without ^foo$, but most will work without the ^$ delimiters and most will not work at all with the ^$ string delimiters. I just need to understand what is going on before I try to put it on the live server.
Options +FollowSymLinks -Indexes
RewriteEngine on
RewriteBase /
RewriteRule designer-homeware index.php
#------------------ Living ---------------
RewriteRule living-room-hallway category.php?collection=1
RewriteRule designer-shelves-storage contentdata.php?collection=1&type=17
RewriteRule designer-cushions-pouffes contentdata.php?collection=1&type=1
rewriterule designer-coat-hat-racks contentdata.php?collection=1&type=19
rewriterule designer-lighting contentdata.php?collection=1&type=25
rewriterule living-room-accessories contentdata.php?collection=1&type=4
#----------------- Eating ----------------
RewriteRule dining-room-eating category.php?collection=2
rewriterule designer-tableware contentdata.php?collection=2&type=5
rewriterule designer-glassware contentdata.php?collection=2&type=6
rewriterule dining-room-accessories contentdata.php?collection=2&type=8
#----------------- Bathing ----------------
RewriteRule ^bathroom$ category.php?collection=5
RewriteRule bathroom-accessories contentdata.php?collection=5&type=11
#----------------- Kids -------------------
RewriteRule ^childrens-decor$ category.php?collection=6
RewriteRule ^designer-childrens-decor$ contentdata.php?collection=6&type=20
#----------------- Garden ------------------
RewriteRule garden category.php?collection=7
RewriteRule garden-accessories contentdata.php?collection=7&type=21
#----------------- Art ---------------------
rewriterule designer-prints contentdata.php?collection=8&type=24
#----------------- Designers --------------
RewriteRule homeware-designers designer.php
RewriteRule homeware-designer/cquoi designerdata.php?search=7
RewriteRule homeware-designer/remember designerdata.php?search=8
RewriteRule homeware-designer/terafue designerdata.php?search=10
RewriteRule homeware-designer/r-bernier designerdata.php?search=11
RewriteRule homeware-designer/c-l-boym designerdata.php?search=21
RewriteRule homeware-designer/andre-klauser designerdata.php?search=22
RewriteRule homeware-designer/shin-azumi designerdata.php?search=23
RewriteRule homeware-designer/alejandro-ruiz designerdata.php?search=24
RewriteRule homeware-designer/tord-boontje designerdata.php?search=25
RewriteRule homeware-designer/sibo-home-concept designerdata.php?search=13
RewriteRule homeware-designer/zafferano designerdata.php?search=14
RewriteRule homeware-designer/klevering designerdata.php?search=15
RewriteRule homeware-designer/dutch-waaren designerdata.php?search=16
RewriteRule homeware-designer/vintage-casa designerdata.php?search=17
RewriteRule homeware-designer/hubsch-denmark designerdata.php?search=18
RewriteRule homeware-designer/serholt-sweden designerdata.php?search=19
RewriteRule homeware-designer/lazis designerdata.php?search=20
RewriteRule homeware-designer/atlantis designerdata.php?search=26
RewriteRule homeware-designer/mage designerdata.php?search=4
For these kind of exact-url rules, it's best to include the ^$ delimiters. The issue with needing the ^$ delimiters is that some rules are subsets of others. For example, the bathroom rule would catch the url bathroom-accessories if you didn't have the delimiters. In general, if you know that's the entire exact url you want, it's best to have the delimiters.
The other issue is these rules are all being checked, even after they match. In most cases, it isn't a problem, but like the bathroom word, some might be getting matched on more than one rule. To fix this, put [L] at the end of the rule, to indicate it's the Last rule if it matches - that is, it says if it matches, be done and exit.
It's common to also include an NC flag, for no-case, to not care if it's uppercase or lowercase.
RewriteRule designer-homeware index.php [NC,L]
#------------------ Living ---------------
RewriteRule living-room-hallway category.php?collection=1 [NC,L]
RewriteRule designer-shelves-storage contentdata.php?collection=1&type=17 [NC,L]
etc.
Try this out and see if it helps. I don't see anything wrong with the mage rule specifically, but the L and delimiters will at least keep the rules from overlapping each other.

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_

.htaccess 301 redirect problem

i try to redirect url's like:
example.com/video/1640/video-name
to
example.com/video/1640/video-name/
i've tried with:
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
but it is not working
my currently htaccess file has only the first line:
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
and videos only loads at
example.com/video/1640/video-name/
url type
i want to redirect the non-backslash url type
example.com/video/1640/video-name
to the correct one (the one with the backslash)
How can i do this?
Your second rule should be RewriteRule ^video/([^/]*)/([^/]*)$ /video/$1/$2/ [R=301,L]
Or you could forgo the redirect totally, and just say RewriteRule ^video/([^/]*)/([^/]*)/?$ video.php?id=$1&title=$2 [L] which will allow both to view your video.
Update FallingBullets is right (see the comments on this answer), his answer better suites the OP's problem, so please ignore this answer (I am leaving it for reference, though).
Maybe you simply have to prefix your pattern with a /?? E. g.
RewriteRule ^/?video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^/?video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
# ^ these ones
instead of
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
since you are anchoring the pattern at the beginning of the path (using ^).

.htacces RewriteRule problem

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]

Resources