Need help with regular expressions - URL redirection - php

I'm trying to redirect an easy to remember url to a php file but I'm having some trouble with the regex.
Here's what I have at the moment:
RewriteRule ^tcb/([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,}) /tcb/lerbd.php?autocarro=$1&tipo=$2&dsd=$3
It is working but only if I supply all 3 arguments. I want the last two arguments to be optional so it either works with only the first or all three. I'm hoping you can help me with this.
Thank you very much.

Adding a ? after something in a RegEx makes it optional. so something like:
RewriteRule ^tcb/([a-zA-Z0-9]{1,})/?(([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,}))? /tcb/lerbd.php?autocarro=$1&tipo=$3&dsd=$4
Notice I introduced a new grouping around the 2nd and 3rd arguments, so the backreferences had to be shifted.
You might also want to put an optional / at the end, too, so it can be used just as if it pointed to a directory...

Here's how I solved the problem. It could be helpful for someone who might stumble upon this question:
RewriteRule ^tcb/([a-zA-Z0-9]{1,})/?(([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,}))?$ /tcb/lerbd.php?autocarro=$1&tipo=$3&dsd=$4

Related

simple mod_rewrite not working

Im kinda new using htaccess and mod rewrite
I got every link to work as i want, but i have a problem when im trying to change the link for the forum everything goes kinda to hell..
This is my code
RewriteRule ^thread/([0-9]+)/([A-Za-z0-9]+)$ sidor/forum/showthread.php?threadID=$1&name=$2 [L]
When i try to get thread/1/forum it works perfectly but when i try to get a longer name like this
thread/2/cs-wont-work-for-me
The htaccess gets me a error 404..
And my links i change so (spaces) gets - and åäö gets aao
Anyone knows what the problem is?
Do you need more code? just post a comment and tell me that then i can try to give you a little more.
thread/2/cs-wont-work-for-me is not matched because you only check for alpha numeric characters ([A-Za-z0-9]+). Include dashes and any other characters you want to match to your regex. This should do what you want:
^thread/([0-9]+)/([A-Za-z0-9\-ÅÄÖåäö]+)$
As a sidenote, I can really recommend htaccess tester to debug issues like this.
You can use as
RewriteRule ^thread/([0-9]+)/(.*) sidor/forum/showthread.php?threadID=$1&name=$2 [L]
Does not check the last sequence of string of specific pattern

Simple mod rewrite with PHP Get values

can someone tell me what in the world I'm doing wrong?
trying to take:
search.php?key=23&category=testing
and make:
/23/testing.html
with this:
RewriteRule ^([^/]*)/([^/]*)\.html$ /?key=$1&category=$2 [L]
For the life of me I can't figure this out...or if there is a better way, please point me in the right direction.
** UPDATE
Ok, after giving up on this I finally contacted the hosting company to see if there was a problem with my domain that was keeping this from working. Here is what they told me:
You do not have a rewrite condition and you only have a simple rewrite. For the code to >always work properly you should normally set a "RewriteCond" with a valid pattern.
You have used the "?" question mark character in your code although this is an escape >character and doing so would only cause a rewrite to index.php based upon your code as >everything after the question mark gets escaped.
Please consider using a "RewriteCond" in your code and also use regexes to properly escape >the "?" character and avoid unexpected behavior.
Can anyone give me some help? Thanks!
RewriteRule ^/([^/]*)/([^/]*)\.html$ /?key=$1&category=$2 [L,R]

Regex for parsing URL

I am working on parsing urls in the form of http://site.com/page/var1/var2 and I am using the following regex to do so:
([^/]+)
Now I understand that this takes all values that are not / but I want to also be able to take variables with escaped slashes in them. When I paste these into my browser they end up looking like this: http://site.com/page/var1//stillvar1/var2 which is equal to var1/stillvar1 and var2. My question is bascially how can I modify this regex equation to catch all values which are not / unless / is followed by a slash.
Hopefully I'm being clear.
Thanks in advance!
How about the following regex?
((//|[^/])+)
Dont clearly understand your requirement but as far as i understand try this (tested in robular)
^\w+\:\/{2}(\w+\.)+\w+(\/{1}\w+)+$

.htaccess, Rewriting language code to PHP vars

I tried to find some examples here but none of them work for me.. I'm a newbie working with R.E.
I need some help on .htacess > RewriteRule. How could I achieve this?
www.mysite.pt/folder/?a=home&id=22 -> www.mysite.pt/folder/index.php?lang=pt&a=home&id=22
www.mysite.pt/folder/en/?a=home&id=22 -> www.mysite.pt/folder/index.php?lang=en&a=home&id=22
Notes:
both with and without "www"
the ?a=home&id=22 is only an example, I would like to append the
whole query-string.
The 'folder' is needed until I release the website cause I have to
test it on my client server (e.g. www.mysite.pt/site_v2/).
Thanks in advance for your help,
Pedro
I can mainly help you out with regex. I did a few htaccess interventions in the past but don't know the details.
www.mysite.pt/folder/?a=home&id=22 => www.mysite.pt/folder/index.php?lang=pt&a=home&id=22
regex
(www\.)?mysite\.pt/folder/\?(.*)
replace with
www.mysite.pt/folder/index.php?lang=pt&$1
www.mysite.pt/folder/en/?a=home&id=22 => www.mysite.pt/folder/index.php?lang=en&a=home&id=22
regex
(www\.)?mysite\.pt/folder/en/\?(.*)
replace with
www.mysite.pt/folder/index.php?lang=en&$1
I recall that the order you specify the rewrite rules is important and AFAIK first one wins.
Why? It could be that multiple regexes match
This is not the case with these 2 regexes but something to be aware of
Do you want to support only EN and PT?
I also expect that you have a lot of image, CSS, files, etc. Will you want EN and PL versions of those as well? Anyway, let's focus on the scripts for now. Here you want to pick up URIs for /somefolder/en/ and then /somefolder/. If you are using the DOCroot .htaccess then you'll need this:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*?)/en/$ $1/en/index.php?lang=en [L,NS,QSA]
RewriteRule ^(.*?)/$ $1/pl/index.php?lang=pt [L,NS,QSA]
Lookup the L,NS,QSA flags in the mod_rewrite documentation. You'll need the NS flag to stop subqueries biting you. The QSA flag merges the lang parameter with the rest of the query list.
If you want a fixed folder then replace the (.*?) and $1 by the fixed folder name. Also remember that your HTML references will need to work as well. e.g. if a page includes an image src="images/mygif.gif" then this will either resolve to <same directory as HTML file>/images/mygif.gif from the browser perspective, e.g. /folder/en/images/mygif.gif so you'll need to figure out where to map these and add the corresponding rules.

htaccess regular expression with a "/"

I'm having a brain cramp. I'm using htaccess to rewrite a page and sometimes the variable that gets passed through will have a / (forward slash) in the variable. Sometimes there will be a slash and sometimes there won't but it is super important that all of this is treated as one variable. I'd really rather not reprogram all my pages with a str_replace() to switch a - for a / and then make a call to a database. For example:
http://www.example.com/accounting/finance.htm
Accounting/Finance is one variable that I need.....it is not in an accounting directory and then there's a page called finance.htm in accounting. So far I've got something like
RewriteRule ^([A-Za-z]+.*[A-Za-z]*)\.htm$ mypage.php?page=$1 [L,NC]
But it doesn't like it.
Can someone help me out?
Thanks in advance.
REPLY TO COMMENTS/ANSWERS
The specific rule that I'm looking for is something like this.....
[start of string]...1 or more letters...[possibility of a / followed by 1 or more letters].htm[end of string]
The two answers given below aren't working...I'm pretty sure it keeps treating it as a directory and not an actual "filename". As soon as I remove the forward slash the page works just fine...
If i get you right, you just need this one:
([A-Za-z/]*)\.htm
it should work with every combination of / or not-/
e.g.
accounting/finance.htm
test.htm
A slash is just another character. Apart from that, your regexp looks unnecessarily complex. For instance, .*[A-Za-z]* is not different from .* and also [A-Za-z] can be shortened to [a-z] if you use the [NC] flag.
Your precise rules are not entirely clear, but you probably want something on this line:
RewriteRule ^([a-z/]+)\.htm mypage.php?page=$1

Categories