.htaccess is there a more efficient way of doing this? - .htaccess

I have URLs in this format
site.com/brochure/12/subcat/subcat/maincat
The only important part of the string to my application is the number directly after brochure
There can sometimes be many subcat false directories so I've had to use many rules like these to make it work
RewriteRule ^brochure/([^/]+)/([^/]+)/([^/]+)/?$ brochure.php?cat_path=$1
RewriteRule ^brochure/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ brochure.php?cat_path=$1
RewriteRule ^brochure/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ brochure.php?cat_path=$1
etc etc - sometimes up to five different rules to allow for the different directory structures.
I'm guessing this can be done in a single rule, anyone kind enough to share their ideas?
Thanks

Since you don't care about any part of the string after your initial ([^/]+), why not just use something like:
RewriteRule ^brochure/([^/]+).*$ brochure.php?cat_path=$1
This will match and group your 12, then quietly match and discard the remainder of the string (.*$).

Related

.htaccess re-write rule breaks on value change

I have the following rule in my .htaccess file and it works fine
RewriteRule ^eat/?([a-zA-Z0-9-]+)?/?([a-zA-Z0-9-]+)?/?([0-9]+)?/?([a-zA-Z0-9-]+)?/?$ /incl/pages/details.php?state=$1&city=$2&ID=$3&name=$4 [NC,L]
I need to modify the rule where the ID ($3) will be a 3 character alpha-numeric (always in caps) value and not a 3 digits as it is now.
I've tried but my rule stops working:
RewriteRule ^eat/?([a-zA-Z0-9-]+)?/?([a-zA-Z0-9-]+)?/?([A-Z0-9]+)?/?([a-zA-Z0-9-]+)?/?$ /incl/pages/details.php?state=$1&city=$2&ID=$3&name=$4 [NC,L]
What am I missing?
To me this looks like you have an issue with a clear separation of the parameters. The way you use the question mark operator to implement a "lazy" rule able to rewrite a variable number of parameters will may issues if the capture groups match the same character sets.
Instead I suggest you specify separate rules for fixed numbers of parameters. That allows to not use that operator to such an extend:
RewriteEngine on
RewriteRule ^eat/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([A-Z0-9]+)/([a-zA-Z0-9-]+)/?$ /incl/pages/details.php?state=$1&city=$2&ID=$3&name=$4 [NC,L]
RewriteRule ^eat/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([A-Z0-9]+)/?$ /incl/pages/details.php?state=$1&city=$2&ID=$3 [NC,L]
RewriteRule ^eat/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /incl/pages/details.php?state=$1&city=$2 [NC,L]
RewriteRule ^eat/([a-zA-Z0-9-]+)/?$ /incl/pages/details.php?state=$1 [NC,L]
RewriteRule ^eat/?$ /incl/pages/details.php [NC,L]
And a question: to you really need the NC flag? Is eat really written is different cases? Because it will meddle with your attempt to differ between groups capturing only upper case letters or upper and/or lower case letters.
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Masking file name with htaccess

I have a client that has several files whose name is (for example) car.php, car_edit.php, car_review.php. These each come with query strings - so car.php?id=1234 or car_review.php?id=321. They would like the file names to be truck*.php rather than car*.php.
I'm hoping there's a way using htaccess to convert the url string to be truck*.php and use the current car*.php files. Also if possible I'd like to forward any page asking for car*.php to truck*.php.
I've done quite a bit of searching and haven't found an answer to doing this particular thing. Does anyone know how I might do this? Thanks.
You need rewrite rules. Try something like:
RewriteRule ^truck(.*).php$ /car.php?id=$1 [NC,L]
Note: This is untested, so may require tweaking.
RewriteEngine On
RewriteRule ^truck(.*)\.php$ /car$1.php [NC]
ought to do it. It should automatically transfer any URL query string like id=xxx over to the car*.php rewritten URL.

Redirect files and folders matching a prefix pattern

I am trying to redirect all requests to subfolders and subfiles in a directory that start with 4 numbers (e.g. 2012) to another directory using the .htaccess file.
For example,
/results/2005-09-19-xxx/* to /event/2005-09-19-xxx/*
and
/results/2005-09-20-file.ext to /event/2005-09-20-file.ext
But not
/results/anything-else/*
From the documents I have looked at I believe I need to use a RewriteCond followed by a RewriteRule. I have been struggling to find a way to both match on the initial 4 numbers and still use them in the redirected link.
I have spent many hours trying to find a solution to this issue, any help or advice would be much appreciated. Thank you.
You don't realy need a rewrite condition, try this rewrite rule :
RewriteRule ^results/([0-9]{4}.*)$ /event/$1 [L,QSA]
This should do the trick:
RewriteRule ^result/([0-9]{4})-([0-9]{2})-([0-9]{2})(.+)$ event/$1-$2-$3$4 [L]
This pattern is: 4 numbers dash 2 numbers dash 2 numbers rest, written in a way that you can easily understand and manipulate at a later date if need be. Everything that fits the pattern will be rewritten - the rest will work as it normally does
Example:
result/2012-02-02-abcd.ext
fits the pattern and will be rewritten to
event/2012-02-02-abcd.ext

htaccess issue, change '/' to '.'

This is my first Question to StackOverflow. I have been working with .htaccess for a PHP application that is currently in development. My Contention is changing a forward slash to a period.
I have had no Joy so far, currently my code looks like the following:
RewriteRule ^([A-Za-z_-]+)?$ app/account/?account_name=$1 [NC,L]
The above is taking away the 'app/account/?account_name=account_name' and allowing displaying the following : www.exampleurl.com/examplename. This is fine, but is there anyway to change my code so instead of /examplename it states www.exampleurl.com.examplename ? I have spent some time googling the answer to no avail.
If anyone can give me some guidance that would be fantastic, thanks in advance.
I can't think of a real replace, but what you can if the nesting is limited to some number (let's say 3) is provide multiple rules each dealing with specific nesting level:
RewriteRule ^([^/]+)/([^/]+)/(.*) /$1.$2.$3 [L,QSA]
RewriteRule ^([^/]+)/(.*) /$1.$2 [L,QSA]
# third rule is not needed, because when there is only one level, there is nothing to replace
What you need to make sure is that the rules are in descending order of the nesting levels (L flag ensures processing is stopped on a level that is matched)

mod_rewrite Redirect Rule Variables question

I'm a bit of an .htaccess n00b, and can't for the life of me get a handle of regular expressions.
I have the following piece of RewriteRule code that works just fine:
RewriteRule ^logo/?$ /pages/logo.html
Basically, it takes /pages/logo.html and makes it /logo.
Is there a way for me to generalize that code with variables, so that it works automatically without having to have an independent line for each page?
I know $1 can work as a variable, but thats usually for queries, and I can't get it to work in this instance.
First you need to know that mod_rewrite can only handle requests to the server. So you would need to request /logo to have it rewritten to /pages/logo.html. And that’s what the rule does, it rewrites requests with the URL path /logo internally to /pages/logo.html and not vice versa.
If you now want to use portions of the matched string, you need to use groups to group them ( (expr)) that you then can reference to with $n. In your case the pattern [^/] will be suitable that describes any character other than the slash /:
RewriteRule ^([^/]+)$ /pages/$1.html
Try this:
RewriteRule ^/pages/(.*)\.html$ /$1
The (.*) matches anything between pages/ and .html. Whatever it matches is used in $1. So, /pages/logo.html becomes /logo, and /pages/subdir/other_page.html would become /subdir/other_page

Resources