Rewrite different page with .htaccess url rewrite - .htaccess

I'd like to use .htaccess for url rewrite.
In my previous post you have shown me how to do it to the index.php page but now would like to do also for other pages like browser.error.php, etc.
To do so I tried to add the last 2 rows in the .htaccess, but unfortunately if I try the link www.mysite/browser-error/ it doesn't work - Return me in the index page.
Options +FollowSymLinks
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(.*)/(.*)/(.*)\/?$ index.php?op=$1&idric=$2&sp=$3 [L,QSA,NC]
RewriteRule ^(.*)/(.*)\/?$ index.php?op=$1&idric=$2 [L,QSA,NC]
RewriteRule ^(.*)\/?$ index.php?op=$1 [L,QSA]
#NEW ADDED ROWS
RewriteRule ^browser-error/(.*)\/?$ browser.error.php?r=$1 [L]
RewriteRule ^browser-error\/?$ browser.error.php [L]
Where I am doing wrong? How would I go? Thank you

I would suggest avoid use of .* and use [^/]+ instead with re-ordering of your rules:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
#NEW ADDED ROWS
RewriteRule ^browser-error/([^/]+)/?$ browser.error.php?r=$1 [L,QSA,NC]
RewriteRule ^browser-error/?$ browser.error.php [L,NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?op=$1&idric=$2&sp=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?op=$1&idric=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?op=$1 [L,QSA]

You have rules before preceding this rule. That makes your petition change to index.php before your rule is reached. (They all have the L flag that makes them be the LAST rule processed). As they are not restricted to not be applied when url is browser-error, they will run on every ///* or simialr petition.
Then, you should add RewriteCond to negate the browser-error url in your other RewriteRules as this:
Please try, and comment if that´s not what you need of it does not work as expected.
Hope it helps
Options +FollowSymLinks
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(.*)/(.*)/(.*)\/?$ index.php?op=$1&idric=$2&sp=$3 [L,QSA,NC]
RewriteCond %{REQUEST_URI} !^browser-error\/?(.*)?\/?%
RewriteRule ^(.*)/(.*)\/?$ index.php?op=$1&idric=$2 [L,QSA,NC]
RewriteCond %{REQUEST_URI} !^browser-error\/?(.*)?\/?%
RewriteRule ^(.*)\/?$ index.php?op=$1 [L,QSA]
#NEW ADDED ROWS
RewriteRule ^browser-error/(.*)\/?$ browser.error.php?r=$1 [L]
RewriteRule ^browser-error\/?$ browser.error.php [L]

Related

Looking for advice on mod_rewrite and htaccess

I am not experienced with htaccess... But I am authoring a site on which I am using a simple redirect. I basically just copied the first htaccess that I found and it works fine, but I am curious if I am using it properly and/or if it is responsible for some problems I am having with the site.
This is my file..
RewriteEngine On
Exclude some directories from URI rewriting
RewriteRule ^(dir1|dir2|dir3) - [L]
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /src/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/src/.*$
RewriteRule ^(.*)$ /src/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^src/.*$ /src/index.php [NC,L]
The index.php (as you can tell) lives in /src), as does most pf the site.
One problem I am having is that accessing some of the includes results in pulling back the index.php!
Any advice, or directions to some tuts or articles on htaccess would be GREATLY appreciated.
Thanks
Have to say that the lot of the code in here looks redundant. Replace your code with this:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} ^/(dir1|dir2|dir3) [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !^/src/.*$
RewriteRule ^(.*)$ /src/$1 [L]
RewriteRule ^src/.*$ /src/index.php [NC,L]
Also when you say that ccessing some of the includes results in pulling back the index.php! I hope you are just including them like:
include('path/to/somefile.php');
That won't go through .htaccess since this inclusion is transparent to Apache.
Try combining a few of the rules:
RewriteEngine On
# Exclude some directories from URI rewriting
RewriteRule ^(dir1|dir2|dir3) - [L]
# block access to the htaccess file
RewriteRule ^\.htaccess$ - [F,L]
# route "/" requests to index.php
RewriteRule ^$ /src/index.php [L]
# route requests that exists in /src/ to /src/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/src%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/src%{REQUEST_URI} -d
RewriteRule ^(.*)$ /src/$1 [L]
# route everything else to /src/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /src/index.php [L]

Multiple RewriteRule

I would like to have multiple RewriteRules in my .htaccess file.
# Enable Rewriting
RewriteEngine on
# Rewrite profile urls
# Input: /user<userId>
# Output: /profile.php?id=<userId>
RewriteRule ^user(\d+)/?$ profile.php?id=$1 [L]
# Rewrite by default to redirect.php
RewriteRule .* redirect.php
Every requests points to redirect.php
I thought, with the [L] flag in the first RewriteRule, would stop processing the rule set.
If you would like to rewrite /user<userId> to /profile.php?id=<userId> and rewrite the other URLs to /redirect.php, then you could try these two configuration directives:
RewriteEngine on
RewriteRule ^user([a-zA-Z0-9_-]+)/?$ /profile.php?id=$1 [L]
RewriteRule .* /redirect.php
OR:
RewriteEngine on
RewriteRule ^user([a-zA-Z0-9_-]+)/?$ /profile.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /redirect.php
Adding the following to the second RewriteRule works.
# Rewrite by default to redirect.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* redirect.php
But im not sure, why this is needed.
[L] flag stops processing only in current iteration but mod_rewrite repeat process until path will not be constant.
In your case I think you can use this rule:
RewriteRule ^user\d+$ profile.php
RewriteCond %{REQUEST_URI} !profile.php
RewriteRule ^.*$ redirect.php

RewriteRule for JS cache busting

I use cache busting query strings on my JavaScript/CSS, and just wondering how I can rewrite that so its part of the filename instead?
I want to rewrite: website.com/js/m/file.min.js?params to website.com/js/m/file.min.params.js instead.
<IfModule mod_rewrite.c>
# Domain Redirects
RewriteEngine On
RewriteBase /
RewriteRule ^/js/m/([a-zA-Z.]+).min.[0-9]+.js$ /js/m/$1.min.js [L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
#If we're here, redirect through zend:
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
First of all your this rule is incorrect and won't work:
RewriteRule ^/js/m/([a-zA-Z.]+).min.[0-9]+.js$ /js/m/$1.min.js [L]
Reason: RewriteRule matches URI without a leading slash.
Now your task:
I want to rewrite: website.com/js/m/file.min.js?params to
website.com/js/m/file.min.params.js instead.
Can be handled by this rule:
# redirect from /js/m/file.min.js?params to /js/m/file.min.params.js
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(js/m/.+?)\.js\?([^\s&]+) [NC]
RewriteRule ^ /%1.%2.js? [R=301,L]
# forward from /js/m/file.min.params.js to /js/m/file.min.js?params
RewriteRule ^(js/m/.+?\.min)\.([^.]+)\.js$ $1.js?$2 [L,QSA,NC]

%2520 (Double space) in URL for URL with spaces

My URL structure is like
http://www.example.com/folder/index.php?dir=dir1
To be able to access it from
http://www.example.com/folder/dir1
and at the same time redirect the 1st URL to 2nd one, my htaccess (in 'folder') is
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]
The trouble is that if any directory name in URL contains a 'space', the URL shows %2520 instead of the 'space'.Please advice in modifying the htaccess so it shows %20 or preferrably a simple 'space'?
try adding a NE on the redirect ie
RewriteRule ^ %1? [L,R=301,NE]
EDIT
Since you've read my htaccess, do you see any possiblity of shortening it further
Below are a couple of comments
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]
#this looks redundant with last rule, and could be deleted?
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
#this says if not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
#and this says if it IS an existing directory
#Is this what you wanted, or should it be not an existing directory i.e
# RewriteCond %{REQUEST_FILENAME} !-d instead
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]

Delete ugly ?pg=1 from URL via .htacess RewriteRule

I have pagination plugin in my CMS which produces ?pg=1 links. I want to redirect this url without this ugly postfix because without it CMS shows first page too.
So, i have url like http://site.ru/category/schock-crane/?pg=1 or http://site.ru/moyka/?type=granit&pg=1
I want to redirect such urls to http://site.ru/category/schock-crane/ and http://site.ru/moyka/?type=granit respectly.
I tried this .htaccess code
RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L]
I tried this regexp code at regexp trainers - it worked. But at live server no redirect happens.
Here is whole .htaccess file:
AddDefaultCharset Off
#DirectoryIndex index.php index.html
Options +FollowSymLinks
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# fix /?pg=1
RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L]
# fix .palitra-tab
RewriteCond %{REQUEST_URI} -tab$
RewriteRule (.*)/([0-9]*)/\.(.*)-tab?$ http://site.ru/redirect.php?id=$2 [R=301,L]
RewriteCond %{REQUEST_URI} ^/csss/.* [NC]
RewriteRule csss/(.*)\.css$ css.php?n=$1 [L]
#RewriteRule ^(.*)/csss/(.*) /test.php [L]
RewriteRule ^(.*)/base([0-9]+)/?$ $1?base=$2
#RewriteCond %{REQUEST_FILENAME} -f
#RewriteRule \.(gif|jpeg|jpg|png)$ /images/watermark/index.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*) index.php
RewriteCond %{HTTP:Authorization} !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
</IfModule>
You need to use RewriteCond to examine the query string:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)pg=1(&(.*)|$)
RewriteCond %1%4 (.*?)&?$
RewriteRule ^ %{REQUEST_URI}?%1 [R=301,L]
The second RewriteCond is just to remove a trailing &.
Maybe the missing ^ or / kills the redirect
# fix /?pg=1
RewriteRule ^(.*)(&|\?)pg=1$ /$1 [R=301,L]
or try
RewriteRule ^(.*)(&|\?)pg=1$ http://%{HTTP_HOST}/$1 [R=301,L]

Resources