RewriteCond - What am I doing wrong? - .htaccess

I'm using Opencart with the SEO Friendly url's turned on.
That's made the .htaccess file look like this
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
But I need to add in a folder exception that doesn't get rewritten, specifically /emailer
Looking around the web at answers I did the following:
RewriteCond %{REQUEST_URI} !^/(emailer|js|css)/ [NC]
RewriteRule ^ - [L]
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
That makes /emailer/ work, but the other rewriting doesn't. Does anyone know what I'm doing wrong here?

You just insert it before the main RewriteRule.
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^\/(emailer|js|css) [NC]
RewriteCond %{REQUEST_URI} !\.(ico|gif|jpg|jpeg|png|js|css)$ [NC]
RewriteRule ^([^?]*) index.php?_route_=$1&request_uri=%{REQUEST_URI} [L,QSA]
Note: It's not tested, but this should give you the idea how to do it.
Later EDIT:
I have tested my code and it didn't work. I have edited it and it works now, like this:
RewriteCond %{REQUEST_FILENAME} !-f => if the request is not a real filename
RewriteCond %{REQUEST_FILENAME} !-d => if the request is not a real directory
RewriteCond %{REQUEST_URI} !^\/(emailer|js|css) [NC] => if there is not a directory/file in your root that starts with the words emailer/js/css
RewriteCond %{REQUEST_URI} !\.(ico|gif|jpg|jpeg|png|js|css)$ [NC] => if the request does not end with an extension of the following ico/gif/jpg/jpeg/png/js/css
Notes:
to test if you need a forward slash or not in your rewrite rule, append a query variable like i did in my modified example and print it (on the first line after the php open tag) in your index.php file like this:
print_r($_GET['request_uri']);
exit;
After you are done testing, remove the request_uri from htacces and index.php.
Always escape the forward slash with a backslash, like this \ /

Related

.htaccess single variable url rewrite

I am trying to redirect http://test.pearlsquirrel.com/explore_all?u=hiphop to http://test.pearlsquirrel.com/explore_all/hiphop using htaccess. However, It keeps redirecting here: http://test.pearlsquirrel.com/explore_all/?u=site_error.php. I have looked all over Stackoverflow for the answer but I just can't seem to find it. I would really appreciate some help in fixing this problem, thanks!
Here is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) explore_all?u=$1 [L]
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} [^=]+=([^=]+)
RewriteRule .* /%1? [L]
Will map this:
http://test.pearlsquirrel.com/explore_all?u=hiphop
To this:
http://test.pearlsquirrel.com/explore_all/hiphop
Replace the corresponding code in your .htaccess file with this one.
I have not checked the rules in your .htaccess file.
OPTION
Now, if it is the other way around, which I think it is, and the idea is to map this incoming URL:
http://test.pearlsquirrel.com/explore_all/hiphop
To this resource:
http://test.pearlsquirrel.com/explore_all?u=hiphop
Replace the above rule set with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([^/]+)/?
RewriteRule .* http://test.pearlsquirrel.com/explore_all?u=%1 [L]

htaccess subdomain ssl wildcard not opening file

Ok So i have come across a problem with my htaccess and how to get it to work.
I have just purchased a ssl wildcard for my primary and sub domains.
I am with bluehost and they suggest adding this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdomain/$1
RewriteCond %{HTTP_HOST} ^subdomain.example.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
At the moment all i have is this...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ https://www.domain.com/$1 [L]
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sub_folder/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
and that works however, i cannot get it so that when i open the file, it just opens the file with no redirects...
So If i open sub.domain.com/file/fil1.js it will open the index.php which is what i do not want...
but if i do /browse or something like that it works...
Ok after much mucking around and playing around it was the file request which wasnt including the subdomain folder, which was causing it to not find the file location. So I managed to do a manual check for the file, and this is what i got. This seems to do exactly what i want. Im not sure if there is a more efficient way, but this is what it got.
#-------------------SUB.DOMAIN.COM---------------------
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{DOCUMENT_ROOT}/sub_folder%{REQUEST_URI} -f
RewriteRule . %{DOCUMENT_ROOT}/sub_folder%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sub_folder/index.php [L]
#--------------------------------------------------------------
instead of
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sub_folder/index.php [L]
You need to change your second rule so that it only gets applied when the request is ^(/)?$, then you need to duplicate what bluehost tells you to do. Essentially, replace:
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sub_folder/index.php [L]
With what they tell you to use:
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdomain/$1 [L]
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteRule ^/?$ /sub_folder/index.php [L]

Htaccess RewriteRule

After asking this question Htaccess URLRewrite to another map
That works great, but now i want to add an extra 'map', but that don't work.
My .htaccess file now:
ErrorDocument 404 /v2/404.php
ErrorDocument 404 /v2/505.php
RewriteEngine on
RewriteRule ^error/([^/]+)/?$ error.php?id=$1 [L,QSA,NC]
RewriteRule ^email/([^/]+)/?$ email.php?id=$1 [L,QSA,NC]
RewriteRule ^activate/([^/]+)/?$ activate.php?code=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^werknemer/([^/]+)/?$ werknemer/$1.php [L,QSA,NC]
RewriteRule ^klanten/([^/]+)/?$ klanten/$1.php [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
Always the last wont work, now RewriteRule ^klanten/ don't work, and when i do RewriteRule ^werknemer/ after RewriteRule ^klanten/, RewriteRule ^klanten/ works but RewriteRule ^werknemer/ not.
How to fix this?
Thanks!
Wouter0100
You need to duplicate the 2 conditions again. RewriteCond's are only applied to the immediately following RewriteRule, so you need to duplicate the conditions if they are to be applied to more than one rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^werknemer/([^/]+)/?$ werknemer/$1.php [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^klanten/([^/]+)/?$ klanten/$1.php [L,QSA,NC]
Maybe you need it for your last rule as well:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
That checks if a trailing slash exists, and to remove it before seeing if adding the php to the end produces an existing file.

mod-rewrite force ?

I need to be able to post links that look like "mysite.com/?d=48YSWD96" & go to file path "/d=48YSWD96.php" So, I need "mysite.com/?d=48YSWD96" to go to "mysite.com/d=48YSWD96". I can't start my file name with a "?" because it will just turn into an index page for the root folder and disregard the content in the file. How do I do this? this is my htacces:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteBase /
Same thing as the query string match from this question (which has the same .htaccess): url construction htaccess
RewriteCond %{QUERY_STRING} (^|&)d=([A-Za-z0-9]+)($|&)
RewriteRule ^$ /d=%2.php [L]

.htaccess how to use RedirectMatch

Since I changed the structure of my website, some of the links became broken.
What rule should I write in .htaccess to fix the problem.
before it was like this
/news/194-kozyrnyj-vecher-premier-ligi.html
and now it looks like this
/news/2-news/194-kozyrnyj-vecher-premier-ligi.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
RewriteCond %{REQUEST_URI} !^/news/2-news/(.*)
RewriteRule ^news/(.*)$ /news/2-news/$1 [R=permanent,L]
should be:
RewriteCond %{REQUEST_URI} !^/news/2-news/(.*)
RewriteRule ^news/(.*)$ /news/2-news/$1 [R=permanent,L]
in your .htaccess file

Resources