Redirect duplicate pages - .htaccess

I would like to 301 redirect redirect all duplicate pages:
example.com/category/pagename-2
example.com/category/pagename-3
example.com/category/pagename-4
...
to original page:
example.com/category/pagename
category is the same for every page
pagename is different for every page
-2, -3, -4, -5... is the same for every page
Can you help? I always have a hard time using .htaccess so I prefer to ask before testing.

You may use this simple rule to redirect as your topmost rule:
RewriteEngine On
RewriteRule ^(category/.+?)-\d+/?$ /$1 [L,NC,NE,R=301]

Could you please try following. I couldn't test it as of now, will test in sometime should work I believe.
RewriteEngine on
RewriteCond %{REQUEST_URI} (^/category/[^-]*)-[0-9]+$
RewriteRule ^(.*) /%1 [NC,L,R=301]

Related

Pagination Redirect with .htaccess

I want to redirect the /page/2 style pagination pages to ?page=2 and to the others in this format.
So any urls like these:
https://www.example.com/dogs/page/2
https://www.example.com/horses/videos/page/3
https://www.example.com/cats/photos/page/4
will be redirected to:
https://www.example.com/dogs?page=2
https://www.example.com/horses/videos?page=3
https://www.example.com/cats/photos?page=4
Could you please try following, written based on your shown samples. Please make sure you clear your browser cache before testing these URLs.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/(.*?)/([^/]*)/([^/]*)/?$
RewriteRule ^(.*)$ %1?%2=%3 [L]
You may use this rule:
RewriteEngine On
RewriteRule ^(.+)/page/(\d+)/?$ $1?page=$2 [L,NC,QSA]

Redirect 301 for URLs with same parameter, different values, different targets

This mod_rewrite works for one URL - /blog/ - I have to target:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^pagenumber=([0-9]*)$
RewriteRule (.*) /blog/? [R=301,L]
The issue I have is that pagenumber can be 2, 3, 4, and so on for a number of different query URLs, such as:
/blog?pagenumber=2
/blog?pagenumber=3
/blog?pagenumber=4
/corp?pagenumber=2
/corp?pagenumber=3
/new-1?pagenumber=2
Is it possible to create a RewriteCond such as:
^/blog(^\?)pagenumber=([0-9]*)$ http://www.domain.com/blog/?
that I can reuse for each individual URL (/blog/, /corp/, etc.)?
(I'm certain that example is not right, but wanted to get the idea across...)
Or is there another, simpler way that I am missing?
Any help that anyone can provide would be most appreciated.
Thank you!
Bryan
To redirect to requested path without query perameter, Replace your Rule with the following
RewriteRule (.*) %{REQUEST_URI}? [L,R]

how to Rewrite Single Page url in .htaccess

I need to rewrite only 1 specific URL
from
http://www.domainname.com/index.php?route=payment/axis/callback
to
http://www.domainname.com/payment/axis/callback
I tried these two from stack overflow, I don't know why its not working
1st one :
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?route=payment/axis/callback [NC,L]
2nd one :
RewriteRule ^index.php?route=payment/axis/callback payment/axis/callback [L]
Try this:
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
See the full page here.
Hope it helps!
I wouldn't use .htaccess with RewriteRule, since there are often problems with it. A simple workaround (with PHP redirect):
<?php
if($_GET['route'] == 'payment/axis/callback') {
header("Location: http://www.domainname.com/payment/axis/callback");
}
?>
You can either use h0ch5tr4355's workaround or you can try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^route=payment/axis/callback$
RewriteCond %{SCRIPT_FILENAME} index.php
RewriteRule (.*) /payment/axis/callback [NC,QSD]
If you instead of a rewrite would like it to redirect to the new url you can add R=301 to the flags of the RewriteRule.

Redirect /index.php from the end of the URL

Have a website with subpages all in this format:
mydomain.com/something
That's fine. But what is NOT fine is that you can also do mydomain.com/something/index.php (you can enter address in this format into your browser) and you still get the content on that mydomain.com/something.
I don't want those two possibilites to be available at the same time, Google doesn't like this. I want just one to be possible.
So what I want to do is whenever you type into your browser mydomain.com/something/index.php, you will be redirected to mydomain.com/something (without that /index.php at the end).
How should I write a .htaccess code to do something like this?
add the following lines to .htaccess in the root directory of your website
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.*?)/?index.php$ /$1 [R=301,L,NC,QSA]
Note: the first condition assure that no previous redirection is made (to prevent redirection loop)
Mordor:
You can try this in your .htaccess file:
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

mod_rewrite so that first-level subdirectory is a GET variable

Alright, title is REALLY sloppy.
Here's my problem: I have a news site and when you go to the main page (domain.com) it redirects you to domain.com/news/top?geography=San_Francisco after it figures out your geography.
How do I use the .htaccess so that it goes from domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top ?
There are some similar questions, but I have not found one similar enough in that you're editing the URL as a furtherback subdirectory.
It should also be noted that I am using the Code Igniter framework for PHP and it normally has it as domain.com/index.php/news/top?geography=San_Francisco but I did a mod_rewrite already to get rid of the index.php. The code is as follows for that:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Code I've tried:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Before the index.php rule that you have, try adding this:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
You'll need to make sure the links you generate are in the form of domain.com/San_Francisco/news/top though.
But to take care of the links in the wild that still look like the old way, you have to match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]
This will 301 redirect the browser if someone goes to the link domain.com/news/top?geography=San_Francisco and make it so the browser's address bar says this: domain.com/San_Francisco/news/top. At which point the browser will send another request for the second URL, and you use the rule above to change it back into the one with a query string.

Resources