.htaccess redirection issue - .htaccess

I suck at htaccess and trying to achieve the following set of redirections. Any help would be greatly appreciated. I can't find anything on askapache and I don't understand general htaccess rules formating.
domain1.com/x -> sub.domain2.com (x being any page and file under this domain, but not the bare domain itself)
domain1.com -> domain2.com (when they type the bare domaine, redirect to the new one)

.htaccess can be used to modify behavior of any directory. Unless you set rule for /x to one directory, you can't use .htaccess. In this case it should be easier to set up redirection in Apache configuration file. Try something like this in Apache configuration file:
RewriteEngine on
RewriteRule ^/$ http://domain2.com/ [R,L]
RewriteRule ^/.+ http://sub.domain2.com/ [R,L]

Related

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]

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 redirection rule

I want to redirect
http://subdom.domain.com/guidelines/article1
http://subdom.domain.com/guidelines/article2
http://subdom.domain.com/guidelines/article3
to
http://subdom.domain.com/notsoguideline/noarticlepage/blahblah
so I wrote the following rule in .htaccess of my wordpress site but it doesn't seem to work
RewriteRule ^guidelines/([^/]+)/ /notsoguideline/noarticlepage/blahblah/
Check that your hosting supports and allows the use of .htaccess files.
Ensure that you have RewriteEngine On in your .htaccess (before your custom rules)
If you create a nonsense .htaccess file what happens? If nothing then your .htaccess file is not in use.

.htaccess - add language sub directory by default "sv/" (redirect)

Trying to orientate myself through the .htaccess jungle, I've now narrated down my rewrite problems to one - adding a language suffix upon entering the page / typing in an address that doesn't have such one.
For example: a visitor types mysite.com/kontakt. This should take them to the default language of the site, i.e. mysite.com/sv/kontakt. Or, just typing in mysite.com should take him/her to mysite.com/sv/.
I'm developing this site locally using MAMP, and the site is located in a subdirectory, and here's the tricky part... How do I sort this out in the .htaccess file?
Current code used:
RewriteBase /mysite/
RewriteCond %{REQUEST_URI} !^(sv|en)
RewriteRule ^(.*)$ sv/$1 [L,R=301]
And that takes me from localhost/mysite/ to localhost/sv/. Not exactly right.
Try this one:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /mysite/
RewriteRule ^(?!(?:sv|en)/)(.*)$ sv/$1 [R=301,L]
This needs to be placed into .htaccess file into /mysite/ folder.
If accessing localhost/mysite/sv without trailing slash, the rule will redirect it to localhost/mysite/sv/sv as it expects that the language part (sv or en) will be followed by the slash /.
P.S.
And look into setting up a VirtualHost -- then there will be much less (if none at all) of such "tricky parts" with accessing your website via http://localhost/mysite/sv/kontakt (I'm sure you will agree, that http://mysite.dev/sv/kontakt sounds and looks better).

.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