301 Redirection of url Pattern in htaccess - .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]

Related

Redirect everything after domain.com/articles.php

So i have this issue, with changing an old site with a new and I need to redirect all the old links. So have this:
domain.com/articles.php?var=1
I basicly want to redirect everything after articles.php to just domain.com, including the /articles.php.
Thank you in advance.
Try adding this to your htaccess file:
RedirectMatch 301 ^/articles\.php$ /
or if you have mod_rewrite rules already in your htaccess file, you need to use mod_rewrite isntead, and add this rule above whatever rules are already there:
RewriteRule ^articles\.php$ / [L,R=301]

Drupal 301 redirects

I am doing a set of 301 redirects in Drupal.
I am using a standard method in the .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
Redirect 301 /user/testimonials http://thesitedomain.com/testimonials
Redirect 301 /user/contact http://thesitedomain.com/contact
</IfModule>
but the return url ends up with "?q=user" and stops it working. eg:
http://thesitedomain.com/about?q=user/about
I am not great at htaccess redirects (obviously) and I am no Drupal expert at all.
Also, if you know of a comprehensive htaccess rewrite resource I would much appreciate reading hat.
You will need to use mod_rewrite instead to strip out existing query string:
RewriteEngine On
RewriteRule ^user/testimonials/?$ http://thesitedomain.com/testimonials? [L,NC,R=301]
RewriteRule ^user/contact/?$ http://thesitedomain.com/contact? [L,NC,R=301]
Take note of trailing ? in target that strips out existing query string.
I can't speak to drupal, but I know you don't need to enclose those redirects in the <IfModule mod_rewrite.c> tags, since they don't use the RewriteEngine, the below would suffice:
Redirect 301 /user/testimonials http://thesitedomain.com/testimonials
Redirect 301 /user/contact http://thesitedomain.com/contact
Are both urls in the same drupal? Or are you moving from a different site?
I mean:
/user/testimonials
http://thesitedomain.com/testimonials
Maybe what you need is to add an url alias for /user/testimonials like /testimonials
See under admin at /admin/config/search/path in drupal 7.
Using .htaccess file is not a good practice, because, in some updates you have to update the .htaccess file too.
You can try GlobalRedirect module to manage your redirects.

htaccess rewrite query string nothing works

THE PROBLEM
After looking at 50+ StackOverflow posts and trying many permutations of my htaccess file, it does nothing still.
WHAT I HAVE TRIED
Using this website to generate my htaccess file: http://www.generateit.net/mod-rewrite/
Setting AllowOverride All in my httpd.conf file and restarting Apache.
MY CURRENT HTACCESS FILE
Lives in the root directory.
RewriteEngine On
RewriteBase /
RewriteRule ^find-a-local-doctor/([^/]*)/([^/]*)$ /find-a-local-doctor/?state=$1&city=$2 [L]
WHAT I WANT TO ACCOMPLISH
Change this URL:
http://www.md1network.com/find-a-local-doctor/?state=FL&city=Tampa
To this:
http://www.md1network.com/find-a-local-doctor/FL/Tampa
ADDITIONALLY
Since the actual file doing the work is: http://www.md1network.com/find-a-local-doctor/index.php, I need to be able to parse the query string with PHP. Hopefully, I will still be able to do this to get the state and city.
Please help.
Thanks.
Your existing rule looks alright but you will need an additional external redirection rule for reverse. Put this rule before your existing rule (just below RewriteBase /).
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(find-a-local-doctor)/(?:index\.php)?\?state=([^&]+)&city=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

htaccess 301 redirect with wildcard to a single page

We took over a website with about a kabillion pages in the old site root directory done in htm that need to be retired. I want to do a 301 redirect from the pages to the index.php in the root directory of the new site using a wildcard. An example of the page naming structure follows:
oldpage_dees.htm
oldPage_dat.htm
oldPage_deeudderting.htm
and so on. As stated, I need them redirected to the index.php in the root directory. Going by examples and discussions here I've tried:
RewriteEngine On
RewriteRule ^/oldpage_([\w]*).htm$ /index.php [R=301,L]
but I get a 404 error.
Any suggestions? Thanks in advance!
As .htaccess is directory level configuration file, you don't need to specify forward slash, I think this will do the job:
RewriteEngine On
RewriteRule ^oldpage_([\w]*).htm$ index.php [R=301,L]
Meanwhile, you can use the following .htaccess tester to debug your rewrite rules.

.htaccess redirect for images from old folder to new folder

I have just moved from Drupal + Wordpress to a site completely built in WordPress.
Duly I have a set of images where the files no longer exist and need to try and keep all the images in the one folder (if possible). Duly I need to send requests for any gif|png|jpg that are for http://www.domain.com/blog/wp-content/uploads/ to http://www.domain.com/wp-content/uploads.
If anyone could help would be appreciated - my .htaccess aint what it once was. Thanks in advance
If you google for "htaccess redirect", the top link is this:
http://www.htaccessredirect.net/
If you use the "301 Redirect Directory" section, you get this code:
//301 Redirect Entire Directory
RedirectMatch 301 /blog/wp-content/uploads/(.*) /wp-content/uploads/$1
As far as I know the target domain should be absolute, so the following might work:
//301 Redirect Entire Directory
RedirectMatch 301 /blog/wp-content/uploads/(.*) http://www.domain.com/wp-content/uploads/$1
Please try this rule in your .htaccess:
RewriteEngine On
RewriteRule ^/blog/wp-content/uploads/(.+)\.(png|gif|jpg)$ wp-content/uploads/$1.$2 [QSA,L]
Try this
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://www.domain.com/blog/wp-content/uploads [NC]
RewriteRule .* http://www.domain.com/wp-content/uploads [NC,L]
You could try and put this
RewriteEngine ON
RewriteRule ^/blog/wp-content/(.*)$ http://newdomain.com/wp-content/$1 [R=301,L]
What I have hated about all the re-write rules and redirect options for .htaccess files is they all rely on hardcoding the path (URI) and/or server for the redirect.
The point of the ".htaccess" files it it should be for the current directory! It could be referenced in a number of different ways, installed on different servers in different locations. So trying it down to a specific location for a simple directory rename is illogical.
The solution is to somehow incorporate the current URI (regardless or where the ".htaccess" location) into the result...
This is my current solution for location independent ".htaccess" redirect for a renamed sub-directory, and even I admit it is not perfect... BUT IT WORKS...
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^OLDdir/.*$ %{REQUEST_URI}::: [C]
RewriteRule ^(.*)/OLDdir/(.*)::: $1/NEWdir/$2 [R,L]

Resources