Weird mod_rewrite behavior - apache

I'm trying to redirect all specific file extensions to the CDN I have setup. Here's what I'm working with:
RewriteRule ^(.*\.(jpe?g|gif|bmp|png|css|js|txt|wmv|mp4|flv|avi|mov|wmv))$ http://cdn-w.domain.com/$1 [L,NC]
For some reason it's only redirecting to (for example http://www.domain.com/images/image.png)
http://cdn-w.domain.com/image.png
rather than
http://cdn-w.domain.com/images/image.png
Why is that?

Try this rule:
RewriteRule \.(jpe?g|gif|bmp|png|css|js|txt|wmv|mp4|flv|avi|mov|wmv)$ http://cdn-w.domain.com%{REQUEST_URI} [R=301,L,NC]

Related

Mod Rewrite link, and disable original

I have a working mod-rewrite.
RewriteEngine On
RewriteRule ^notifications$ /notifications.php [L]
However, I've seen on some sites, their links seems to be all like website.com/login website.com/notifications website.com/profile
I think I have achieved the same results with my mod-rewrite. But how can I prevent users from entering my /notifications.php page like how some other website does?
Thanks!
Add another rule that looks for a direct request for the php file and redirect it to the non php URL:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /notifications\.php
RewriteRule ^ /notifications [L,R=301]

.htaccess mod_rewrite redirect without query string variables not working

I'm having a problem with one of my rewrite rules. I would like to redirect all of the following URL's to another URL without the query string.
/gallery/products.aspx?C=9&SC=&ID=428&P=10
/gallery/products.aspx?C=2&SC=2&ID=128&P=1
/gallery/products.aspx?ID=147&C=2&SC=&P=7
/gallery/products.aspx?ID=1337&C=15&SC=&P=1
/gallery/products.aspx?ID=1532&C=3&SC=&P=2
/gallery/products.aspx?C=9&SC=&ID=1489&P=1
/gallery/products.aspx?C=7&SC=&ID=100&P=2
/gallery/products.aspx?C=2
/gallery/products.aspx?ID=1328&C=14&SC=11&P=17
/gallery/products.aspx?C=1&SC=&ID=767&P=3
/gallery/products.aspx?ID=1270&C=1&SC=&P=26
and I have this in my .htaccess file
RewriteRule ^gallery/products.aspx http://www.domain.com/category/? [L,R=301]
but it's not working. I checked it in a .htaccess simulator and it found the rule then redirected, but when I upload to my server, it doesn't redirect. I've also tried some other rules with no luck
I was finally able to make this work with the following:
RewriteCond %{HTTP_HOST} www.domain.com [NC]
RewriteRule products.aspx http://www.domain.com/category? [L,R=301]

RewriteRule for URLs that do not end with dot html/php

I would like to redirect all:
to
I have started to
RewriteRule !\.(html|php)$ /get.php?file=$1 [PT]
But it does not work.
I've tested this regexp in PHP it should work in mod_rewrite but I have not tested it and seems to work in mod_rewrite too:
RewriteRule ^(.+)(?<!\.php)(?<!\.html)$ /get.php?file=$1
The rule will rewrite all URLs except those that end with .php or .html.
The most basic way to handle that is:
RewriteRule ^(.*)$ get.php?file=$1 [QSA,L]
Which will redirect everything, setting file to the value of the path.

Trouble with advanced .htaccess redirect

I'm migrating a custom coded blog over to Wordpress, and have to set up a redirect that will handle all of the blog posts.
I need to redirect from this:
/oldblogdirectory/Old_Blog_Posts_Looked_Like_This.htm
to:
/newblogdirectory/new-blog-posts-look-like-this/
Any ideas on the regex for a redirect like this?
Gumbo's approach is certainly the way to do it. I made two test directories:
oldblogdir/archives/blog_posts_look_like_this.htm
newblogdir/archives/blog-posts-look-like-this
And the following RewriteRules redirect successfully. They are only slightly changed to Gumbo's proposal:
RewriteEngine on
RewriteBase /
RewriteRule ^(oldblogdir/archives/[^_]*)_(.*) $1-$2 [N]
RewriteRule ^oldblogdir/archives/(.*?)\.htm$ newblogdir/archives/$1 [R,NC,L]
Note that the [N] causes the .htaccess file to be re-evaluated until the RegEx no longer matches. Therefore you should put it at the very top of the file.
Try this mod_rewrite rules:
RewriteEngine on
RewriteRule ^(oldblogdirectory/[^_]*)_([^_]*)_(.*) /$1-$2-$3 [N]
RewriteRule ^(oldblogdirectory/[^_]*)_(.*) /$1-$2
RewriteRule ^oldblogdirectory/(.+)\.htm$ /newdirectory/$1/ [L,R=301]
But for the uppercase to lowercase conversion you’ll either need a mapping like the internal tolower function or you use PHP for both.

Apache RewriteRule not working without Page # specified

I have a rewrite rule set up in my .htaccess file:
RewriteRule ^Crocodile-Style/([0-9]+)/?$ products/display.php?folder=crocodile-style&page=$1 [L,NC]
http://test.bradp.com/drupal/Crocodile-Style/1 works OK.
http://test.bradp.com/drupal/Crocodile-Style/ DOES NOT WORK.
Apache throws a 404. The PHP logic defaults to page 1 without a page specified, so I know the script is fine.
What can I do?
Thanks
Nick
It's probably easiest to implement this with two rules:
RewriteRule ^Crocodile-Style/?$ products/display.php?folder=crocodile-style [L,NC]
RewriteRule ^Crocodile-Style/([0-9]+)/?$ products/display.php?folder=crocodile-style&page=$1 [L,NC]