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

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.

Related

Where is my mod rewrite fail?

I have got url
ipaddr/opensys/base/
and some other similar, like
ipaddr/opensys/base/something.php?olol=yep
I want to remove from url "opensys" and display there "center", i want to see this links working:
ipaddr/center/base/
and
ipaddr/center/base/something.php?olol=yep
I did it with symlinks, but it was not good, because system is very difficult and with symlink some plugins not works, I want to do it with .htaccess only, after it all will be ok.
My htaccess is:
but links, which i want to see is not working, why?
RewriteEngine on
RewriteRule ^/opensys/base/(.*)$ /center/base/$1 [L]
RedirectMatch 301 ^/opensys/base/(.*)$ /center/base/$1
Redirect works good, but i see 404, rewrite rules is not working. Why?
You also need to handle the /center/base/ now in the .htaccess file as you must be handling opensys earlier. Something like this -
RewriteRule ^/opensys/base/(.*)$ /center/base/$1 [R=301]
RewriteRule ^/center/base/(.*)$ /index.php?args=$1 [QSA,L]
You can use this code in root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} /opensys/(base/\S*)\s [NC]
RewriteRule ^ /center/%1 [R=301,L,NE]
RewriteRule ^center/(base/.*)$ opensys/$1 [L,NC]

Weird mod_rewrite behavior

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]

.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]

htaccess rewrite

I've rebuilt a site using a CMS and I want to make the old urls point to the new pages. I'm having trouble because the old URL looks like this: ?secc=country_club. For instance, domain.com?secc=country_club.
I would like to either have a rule for each url or have it rewrite the ?secc=country-club to just country-club
This is what I have tried, without any success:
RewriteRule ^secc-([^-]*)$ /?secc=$1 [L]
I think it has something to do with the ? in the url
Also if it helps, I am using joomla and I do have sh404sef.
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^secc=(.+)$
RewriteRule ^(.*) %1? [R,L]
This will redirect http://example.com/?secc=MYPAGE to http://example.com/MYPAGE
I think you meant to write '=' after ^secc:
RewriteEngine on
RewriteRule ^?secc=(.*)$ "$1" [QSA]

Strange 301 redirect problem

I'm trying to redirect all URLs that start with "/?page=" to "/stuff/?page="
I have this in my .htaccess file:
RewriteEngine on
RedirectMatch 301 ^/?page=/(.*)$ http://www.mysite.com/stuff/$1
But it's not working.. What am I doing wrong?
Try this
RewriteRule ^/stuff/?page=$ /?page=/
Remember, you're effectively turning the right (of the space) into the left.
The directives of mod_alias (one of them is RedirectMatch) do only work on the URI path and not the query. If you want to inspect the query, use mod_rewrite instead:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=
RewriteRule ^$ /stuff/ [L,R=301]