How to redirect a folder in htaccess - .htaccess

I have a website that has some pages have the same content in them.
Link 1 :
http://abcxyz.com/abc/xyz/1233.html
Link 2 :
http://abcxyz.com/xyz/1233.html
Two links above is one page. Now I want to redirect link 1 to link 2 with htaccess. But, there is a problem, abc in link 1 is not static. Some links are abc, some others are rpq , yklm...
Please help me.

You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^[^/]+/(xyz/.+?\.html)$ /$1 [L,NC,R=302]

Related

htaccess redirect url into subfolder and hide subfolder in url, for only 1 page

.htaccess problem:
I want domain.com/page5 to bring up,
domain.com/folder/page5
but still show domain.com/page5 as the url.
Let this only work for this exact page, and nothing more.
Try the following in root/.htaccess
RewriteEngine on
RewriteRule ^page5$ /folder/page5 [NC,L]

redirect all pages with htaccess

I've been looking to find a solution for this problem but I believe I'm not asking the right question so I'll explain it in detail here.
I built a new HTML site and I want to redirect the old domain which was a PHP site to the new domain which is going to be a HTML site. URL structure is same but file format is changed.
I need a .htaccess code that redirects all pages from old domain to pages of the new domain.
for example: oldsite.com/contact.php ===> newsite.com/contact.html
any ideas?
thanks
Try adding this rule to the htaccess file in your oldsite.com's document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} oldsite\.com$ [NC]
RewriteRule ^(.*)\.php$ http://newsite.com/$1.html [L,R=301]

Mask folder in output URL

I have this website, it is my primary domain, however the folder is actually /public_html/annexation.ca. This part is fine, I have an htaccess rule in my public_html folder to deal with that, so that it shows up as annexation.ca and not annexation.ca/annexation.ca.
But I also run a forum off of that website. The problem is that the phpbb software has an auto-direct after logon, logoff and in a couple of other instances. And when it does so it generates a URL like this:
http://annexation.ca/annexation.ca/community
This is not only cosmetically disruptive, but it also impacts the comment section at the bottom of each unique page, where I have Disqus running. What I need is to add a rule to the htaccess file in the root folder that will make these redirects be automatically translated into this again:
http://annexation.ca/community/
Thanks!
Try adding this to your .htaccess
## 301 Redirect Entire Directory
Redirect 301 /annexation.ca/community(.*) /community/$1
You can try this rule on top of other rules:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+annexation\.ca/(community)([^\s?]+) [NC]
RewriteRule ^ /%1 [L,R=301]

301 Redirection of url Pattern in htaccess

Previously I have vbulletin forum installed on main domain. Now I have replaced it with WP blog and shifted forum to subfolder. Both WP and vB has seperate htaccess files. Please help me to redirect old forum urls to new ones.
Old url pattern:
www.domain.com/f1/post-title/
www.domain.com/f2/post-title/
www.domain.com/f3/post-title/
New url pattern:
www.domain.com/forums/f1/post-title/
www.domain.com/forums/f2/post-title/
www.domain.com/forums/f3/post-title/
Please somebody help me with rewriting rules for correct redirection. Also mention which htaccess (WP or vB) to put the code. Thanks in advance.
It needs to be placed on the .htaccess on the root folder of your domain.
So if your root folder is /home/youraccount/public_html/ then in the .htaccess in that folder.
This will redirect as you asked above, any forum/topic to forums/forum/topic
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^(f\d+)/([^/]+)/?$ /forums/$1/$2/ [R=301,L]
I could use ([^/]+) twice but since you mentioned you have a WordPress in the root now then you should need a more specific rule for the first folder like the above.
This will match the forum id aka f1, f2 ... up to any amount of numbers:
(f\d+)
This will get anything not a / so it will get the post id and title altogether.
([^/]+)
If you have more rules inside your .htaccess file make sure you place this rule after RewriteEngine on and before any other rule, so it doesn't conflict with other rules and redirect as you asked:
RewriteRule ^(f\d+)/([^/]+)/?$ /forums/$1/$2/ [R=301,L]

htaccess rewrite first page of pagination to original url

I'd like to use htaccess to rewrite or redirect all urls that have the string Page-1- and make them go to the original url. e.g.
original url : http://www.website.com/blahblah
assuming the above url is a category with many pages and user has gone to page 2 and through that page he/she wants to return to page 1. As it is now, hitting page 1 at the pagination list , it will take him/her to
http://www.website.com/blahblah/Page-1- (number of results per page)
I'd like to make all urls with Page-1- go to the original url instead
Any help will be GREATLY appreciated
Try:
RewriteEngine On
RewriteRule ^(.*)/Page-1- /$1 [L,R]
Try with QSA
RewriteEngine On
RewriteRule ^(.*)/Page-1- /$1 [L,QSA,R=301]

Resources