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

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]

Related

Remove Query String from 301 Redirect through htaccess

I am facing a couple of 301 redirect issues.
Issue 1:
I want to redirect some links to another URL of the same site:
Redirect 301 /path-example.html /dir/subdir/
The redirect is working but it's generating query string like this:
example.com/dir/subdir/?str=path-example
How do I remove the query sting ?str=path-example ?
Issue 2:
I want to redirect some links to the homepage:
Redirect 301 /some-path.html /
But it's leaving a ? after a trailing slash like this:
example.com/?
How do I remove /? from the forwarded URL?
Existing rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^s=(.*)$
RewriteRule ^$ / [R=301,L]
Thanks!
Please place these rules top of your htaccess files(in case you have more rules in it). Please make sure to clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^index\.php/?$ - [L,NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
##Redirection with removing query string rule here.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\?\S+\s [NC]
RewriteRule ^ /dir/subdir? [R=301,L]
##Redirection to site's home page here for html urls.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\s [NC]
RewriteRule ^ /? [QSD,R=301,L]
RewriteCond %{QUERY_STRING} ^s [NC]
RewriteRule ^/?$ / [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
EDIT: My first answer without OP's other rules set is as follows:
RewriteEngine ON
##Redirection with removing query string rule here.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\?\S+\s [NC]
RewriteRule ^ /dir/subdir? [R=301,L]
##Redirection to site's home page here for html urls.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\s [NC]
RewriteRule ^ /? [QSD,R=301,L]

.htaccess rewrite portion of URL

I have two requirements:
I want to rewrite component/testcomp in URL to just comp.
https://www.website.com/component/testcomp should look as:
https://www.website.com/comp in URL.
All requests coming fo website.com should be 301 redirected to https://www.website.com
Below is the content of my .htaccess file:
IndexIgnore *
Options +FollowSymlinks
Options -Indexes
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
RewriteBase /
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
RewriteRule ^(.*)$ https://www.website.com/$1 [R=301,L]
How can I do so? I tried adding Rewrite Rule but it is not working. Let me know the Rewrite rule & where should I place it.
To rewrite "/component/testcomp" to just "comp", I think you have to flag it with R, or use R=301 to make it a permanent. I have a feeling you may have to adjust additional rules, however.
RewriteRule ^component/testcomp/(.*)$ /comp/$1 [L,R]
or to make it permanent:
RewriteRule ^component/testcomp/(.*)$ /comp/$1 [L,R=301]
Try the following:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /component/test(comp)(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,QSA]
and then,
RewriteRule ^comp(.*)$ /component/testcomp$1 [QSA,NC,L]

Rewrite trailing slash everywhere

I am having problems with my .htaccess file.
It looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^website\.de [NC]
RewriteRule ^(.*)$ http://www.website.de/folder/$1 [L,R=301]
RewriteRule ^traffic/?$ traffic.php [NC,L]
RewriteRule ^what-if/?$ whatif.php [NC,L]
If I call www.website.de/folder/traffic I get www.website.de/folder/traffic/.
But if I call www.website.de/folder/what-if the trailing / is missing.
How can I add it everywhere?
You have to specify a RewriteBase since your htaccess is in a subfolder.
Also, you have to disable MultiViews option to avoid the problem you have.
Options -MultiViews
RewriteEngine On
RewriteBase /folder/
RewriteCond %{HTTP_HOST} ^website\.de$ [NC]
RewriteRule ^(.*)$ http://www.website.de/folder/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+)$ $1/ [R=301,L]
RewriteRule ^traffic/$ traffic.php [NC,L]
RewriteRule ^what-if/$ whatif.php [NC,L]

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]

htaccess - redirect to ignore nested subdirectory and if possible make entire site non-www

I have an issue that's too complex for me to handle, but I'm betting someone has had to do this before, so please let me hear from you. ;)
Here's the situation:
I've got 1 main domain with 3 subdirectories that are nested within each other
(from top to bottom)
http://main-domain.com
then
http://main-domain.com/company-name/
then
http://main-domain.com/company-name/blog/
There's currently 3 .htaccess files -- 1 in each of the 3 directories shown above.
What's the problem?
Instead of having www.main-domain.com/company-name/blog/whatever, I'd like to have main-domain.com/blog/whatever
So, I want to drop the www AND more importantly, drop the middle subdirectory; i.e. /company-name/
I hope that the following examples will help to illustrate the point.
http://main-domain.com/company-name/index.php should be changed to http://main-domain.com/index.php
http://main-domain.com/company-name/blog/my-first-article/ should be changed to http://main-domain.com/blog/my-first-article/
Why do I need this?
I need a shorter URL that is more SEO-friendly. I have too many backlinks that use the 'old' urls, so I need to mod-rewrite them all.
Here are My Current 3 htaccess files
root htaccess: main-domain.com
#Bypass InoCore Templating System
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /reservations/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /reservations/default.php [L]
Options -Indexes
</IfModule>
#END Bypass
#301 REDIRECT
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^info.php - [L]
RewriteCond %{HTTP_HOST} ^www.domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^main-domain.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.main-domain.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
company-name htaccess: main-domain.com/company-name/
RewriteEngine on
RewriteRule ^maping.php /maping.php
RewriteRule ^$ index.php?$1 [L]
RewriteRule (.*) index.php?$1 [L]
#php_flag magic_quotes_gpc off
#BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /company-name/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/index.php [L]
</IfModule>
#END WordPress
blog htaccess: main-domain.com/company-name/blog/
RewriteEngine off
#BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /company-name/blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/blog/index.php [L]
</IfModule>
#END WordPress
Your correct and compact root .htaccess should be like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^info.php - [L]
# match all the domains in single condition while www. is optional
RewriteCond %{HTTP_HOST} ^(www\.)?(domain1|domain2|domain3|main-domain)\.(com|tld)$ [NC]
RewriteRule ^company-name/(.*)$ http://www.domain.tld/$1 [R=301,L,NC,NE]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
NC is for ignore case comparison
$1 is your REQUEST_URI matching group

Resources