RewriteRule redirect - apache

Okay I have this RewriteRule which is supposed to redirect any request for the file base.css to {folder of .htacces file}/include/style/base.css, but is just keeps redirecting in an infinite loop, I thought the L parameter would make sure that wouldn't happen.
RewriteRule (.*)/base.css$ include/style/base.css [L,NC,R=301]
Also it redirects to http://localhost/C:/somemaps/include/style/base.css which it isn't really supposed to do either.
Can anyone tell me how to fix this?
Also I would like to have the RewriteRule so it would redirect any file.css to {folder of .htacces file}/include/style/file.css
BTW the .htacces file is in the root of the website (which is not the root of the server!)

You have Redirect and Rewrite confused. A redirect is a HTTP status code that tells the browser to go to another URL. You actually just want to Rewrite the location to another file location. Try
RewriteRule (.*)/(.*).css$ /include/style/$2.css [L,NC]
If this doesn't work try adding the following right after the RewriteEngine On
RewriteBase /my-virtual-folder-path-where-htaccess-is-stored

Also I would like to have the RewriteRule so it would redirect any file.css to {folder of .htacces file}/include/style/file.css
Try this:
RewriteRule ([^/]+).css$ /include/style/$1.css [L,NC]

This R=301 makes a new request. Therefor it evaluates the RewriteRule again.
Try to exclude this path/directory with a rewrite condition (RewriteCond).

Related

Opencart 301 Redirects

Having a problem with redirect in a .htaccess file on an Opencart store.
It seems that any URL with /index.php?_route_= isn't getting redirected.
For example, this works:
redirect /old-url-here http://example.com/new-url?
This doesn't:
redirect /index.php?_route_=some-url.asp http://example.com
Any idea or suggestions as to what might get this to work?
You can't match against the query string using mod_alias' Redirect directive. You'll need to use mod_rewrite, and if you use mod_rewrite, you're probably going to want to stop using mod_alias altogether.
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} route=some-url\.asp
RewriteRule ^index\.php$ http://example.com/
Another thing is - apart from Jon's answer - that URLs like index.php?_route_=some-keyword are used/created only internally and only in case you have the SEO turned on. In this case, you click on a link with URL like http://yourstore.com/some-keyword and this URL is rewritten into index.php?_route_=some-keyword.
Therefore you shouldn't be creating URLs like that on your own nor try to redirect from them. If you need to redirect, catch the first SEO URL to redirect it.
We do not know where did you put your changes into the .htaccess file, but if you take a close look at this rewrite rule
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
not only you'll find out it is the last rule there, but it also has this L switch which is telling Apache server that if this rule is matched, do a URL rewrite(, attach the query string [QSA] and) stop matching other rules [L] - this is the Last one.
If you need to redirect only a specific URL, do it the same way as redirect for sitemap.xml is done (for example) and place it before the last rewrite rule:
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
# ...
# your new rule here - while the URL in the link is http://yourstore.com/some-url.asp
RewriteRule ^some-url.aspx$ index.php?route=some-folder/some-controller [L]
# ...
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

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

mod_rewrite - simple redirect to parent directory

What I need is to redirect everything from a certain directory to the index file of the parent directory. The URL in the browser has to change as well, just straight redurect. How do I do this? I tried variations of
RewriteRule ^(.*)/?$ ../ [R]
but it displays an error and the actual system path!
Most easily accomplished by using an absolute http URL:
# We assume this is in .htaccess inside the directory to be redirected
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/parent
RewriteRule ^(.*)?$ http://%{HTTP_HOST}/parent [R=301,QSA,L]
Note, QSA was added to preserve the query string, but that can be removed if you don't want it.
You can use redirectMatch:
RedirectMatch 301 ^/child/?$ http://example.com/

Redirect site with .htaccess but exclude one folder

I want to 301 redirect an entire website, but exclude everything in a folder called /uploads which exists in the /root directory.
I have googled for this, but didn't come up with anything, or I didn't think what I saw was right.
Can we crack this?
Try this mod_rewrite rule:
RewriteEngine on
RewriteRule !^uploads($|/) http://example.com%{REQUEST_URI} [L,R=301]
This rule does match any URL path that does not begin with either /uploads or /uploads/ (leading / is missing in the pattern due to the path prefix removal when used in .htaccess files) and redirects the request to the corresponding path at example.com.
Simple answer I just stumbled upon myself.
At the top before any other calls add the following
RewriteRule ^(uploads) - [L]
I think you want this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/uploads/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
If you get 500 Internal Error then double-check that you have a space between } and ! on the second line.
A mod-alias based solution
Redirect all except a specific folder
Add the following line to your root/.htaccess :
RedirectMatch 301 ^/((?!uploads).*)$ http://newdomain.com/$1
This will redirect all pages (excluding /uploads/*) from your old domain to the newdomain.

.htaccess url rewriting /project/Login to /project/public/index.php/Login without redirecting URL

I have this .htaccess code that works perfectly:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ public/index.php [QSA,L]
</IfModule>
...but this code redirects to public sub directory. I don't know if it's possible to rewrite url without redirecting, just use /project/Login appointing to /project/public/index.php/Login.
RewriteRule project/Login$ project/public/Login [L]
Have you tired something like that? Your second link doesn't seem right "/project/public/index.php/Login", you shouldn't specify 'index.php' followed by another folder unless you have a RewriteRule that can handle it, otherwise the page doesn't exist on your server.
'/project/public/index.php?Login' (same as '/project/public/?Login') would be valid though, having the query string accessible as $_GET['Login'].
Hope this helps.