Mod Rewrite - htaccess - .htaccess

Menu code:
Level 1
Level 2
Level 3
Level 4
Current mod working fine on /category/18/cat-name-level/cat-name-level2/
Here is my .htaccess:
RewriteRule ^category/([0-9]+)(?:/([^/]+)(?:/([^/]+))?)(?:/([^/]+)(?:/([^/]+))?)?/$ ./category.php?pid=$1 [QSA,L]
The problem, we can type anything after the ID:
/category/18/yehahh/jsidfd/
/category/18/jkasjksd/dhgidg/ondsg/djgn/
How to fix it?

You'll have to check that the attributes after the ID correspond to the category directly in PHP, and throw a 404 if they don't (so search engines won't index the faulty URLs).
There's no way to check this directly in the .htaccess, unless you generate it and use one RewriteRule for each category.

Restrict the regex for the pid to just numbers. Something like...
\/[0-9]+?$
Then, the last thing HAS to be a numeric value. The ? is almost unnecessary here, but I always err on the side of matching less possibilities, rather than more. This solution assumes pid is an unsigned integer, of course.

Related

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)

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

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 (.*$).

help regarding dynamic redirect rule in htaccess

I need ur help for given subject.
I am playing with htaccess rules first time in life.
here is the scene -
i want to redirect the urls -
http://www.abc.com/var1
http://www.abc.com/var2
to follwing urls -
http://www.abc.com/index.php?u=var1
http://www.abc.com/index.php?u=var2
In this case the values var1 & var2 can be anything (i.e. string which will contain only alphanumeric characters.)
One more thing -
I want to skip this rule if the url is -
http://www.abc.com/SKIPME
Please help me to write this rule!
Regards,
Shahu!
You would be better off defining a URI schema that tells you when something WILL be rewritten. For example...
http://www.abc.com/site/var1
http://www.abc.com/site/var2
That way, you can ensure you only ever apply the rule if the psuedo "site/" directory is browsed and not affect any other URI. This is the rewrite rule based on the above schema.
RewriteEngine on
RewriteRule ^site/([^/\.]+)/?$ index.php?u=$1 [L,NC,QSA]
Any other address, other than "/site/.../" would be unaffected by this rule, which means you don't need to worry about setting some addresses to be avoided. This keeps things as simple as possible.
You don't have to use "site" - you can use whatever name fits your purpose.

.htaccess Rewrite Question

I have a number of pages in my site, as one would expect.
For example:
index.php
submit.php
view.php?id=blah
I want these rewritten like
index/
submit/
view/blah
Whats the best way of doing this?
The ways of handling it through .htaccess Rewrite can generate a bit of a headache. It seems like a basic answer, but unless you're up on your regular expressions, you're going to be lost.
There's a few ways of handling it, however. I'm assuming that you only have index.php, submit.php, and view.php with an id associated.
RewriteEngine On
RewriteRule ^(index|submit|view)/(\d+)$ /$1.php?id=$2
RewriteRule ^(index|submit|view)/(\d+)$ /$1.php
Here's how it works: You tell .htaccess to turn on the Rewrite Engine. Step 2, you give the site the parameter that tells it how it's done. The parameter in this case reads: At the beginning of the url, after the domain name, check for index, submit, or view. If those exists, it'll look for the id. If both those exists, it will return the value into PHP as /(index, submit, or view)?id=$id.
The second one is in case the ID isn't viable.
This is a simple way of handling it. A more complex way of handling it would be...
RewriteEngine On
RewriteRule ^([a-z]+)/?(\d+)?$ /$1.php?id=$2
This will load whatever is written in regular alphabetical characters of upper and lower case letters only, use that as the filename, then detects if id is even necessary--it will load without.
You should be sure to include some safeguards on your $_GET lines to return errors if the names are erroneous or doesn't return anything of worth.
You should play around with it, research Regular Expressions over a pot of coffee and something alcoholic (I believe that regexp is the #1 cause of alcoholism in modern programmers, but I could be wrong ;-P ...) til you find a scheme that fits comfortably into your system.
As a side-note, you can have as many RewriteRules as you need, but they always get processed from the top one first. I realize this sounds like common sense, but it's important to know when debugging.

Resources