Redirect files and folders matching a prefix pattern - .htaccess

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

Related

Forward slashes in part numbers are breaking mod_rewrite rule

I've a simple mod_rewrite rule
RewriteRule ^product/([^/.]+)/([^/.]+)/?$ product.php?partNumber=$1&partName=$2 [L]
It works great for 99.99% of products but there are 3 or 4 products which have a forward slash in their part number (eg PART001/1)
which rewrites to something like:
/product/PART001/1/part-name-here-for-nice-seo
Obviously this doesn't work as it's looking in an extra directory. I need the part number passed correctly as it's used to look up the index in the database and fetch all the product's information.
Is there any way round this?
Looks like you just need to change the ([^/.]+) grouping in your regex to also match slashes. Try:
RewriteRule ^product/(.+)/([^/.]+)/?$ product.php?partNumber=$1&partName=$2 [L]

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.

.htaccess and dynamically generated SEO friendly URLs

I'm trying to build a website that may be called from the URL bar with any one of the following examples:
domainname.com/en
domainname.com/zh-cn/
domainname.com/fr/page1
domainname.com/ru/dir1/page2
domainname.com/jp/dir1/page2/
domainname.com/es-mx/dir1/dir2/page3.html
These page requests need to hit my .htaccess template and ultimately be converted into this php call:
/index.php?lng=???&tpl=???
I've been trying to make RewriteCond and RewriteRule code that will safely deal with the dynamic nature of the URLs I'm trying to take in but totally defeated. I've read close to 50 different websites and been working on this for almost a week now but I have no idea what I'm doing. I don't even know if I should be using a RewriteCond. Here is my last attempt at making a RewriteRule myself:
RewriteRule ^(([a-z]{2})(-[a-z]{2})?)([a-z0-9-\./]*) /index.php?lng=$1&tpl=$4 [QSA,L,NC]
Thanks for any help,
Vince
What's causing your loop is that your regex pattern matching /index.php. Why? Let's take a look:
First, the prefix is stripped because these are rules in an htaccess file, so the URI after the first rewrite is: index.php (query string is separate)
The beginning of your regex: ^(([a-z]{2})(-[a-z]{2})?), matches in in the URI
The next bit of your regex: ([a-z0-9-\./]*) matches dex.php. Thus the rule matches and gets applied again, and will continue to get applied until you've reached the internal recursion limit.
Your URL structure:
domainname.com/en
domainname.com/zh-cn/
domainname.com/fr/page1
domainname.com/ru/dir1/page2
domainname.com/jp/dir1/page2/
domainname.com/es-mx/dir1/dir2/page3.html
Either has a / after the country code or nothing at all, so you need to account for that:
# here -------------------v
^(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$
# and an ending match here ------------^
You shouldn't need to change anything else:
RewriteRule ^(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$ /index.php?lng=$1&tpl=$4 [QSA,L,NC]

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)

Stripping "&Itemid=XX" out of my URLS using .htaccess

I need to strip out a certain part of my URLS being generated and want to use .htaccess.
Some links are being appended with "&Itemid=XX" after the .html.
Example:
http://www.site.com/conferences-and-events.html&Itemid=XX
XX could be one digit or four so I guess I need a wild card for that part. I know other questions have been answered related to stripping out certain parts of URLs using .htaccess but I can't seem to figure out how to get my specific string stripped out. Any help would be appreciated and sorry for being redundant and dense.
You'll want to use URL rewriting for that, something like this should work;
RewriteEngine On
RewriteRule (.*)&Itemid=\d{1,4}(.*) $1$2 [R]
Explanation: This regular expression matches anything ((.*)) followed by &Itemid= [1 to 4 decimals], followed by anything (another (.*)), and redirects ([R]) to the first anything concatenated with the second anything, thus taking out the &Itemid=xx part.

Resources