.htaccess redirect for subdirectory - apache

I have a help section on my site that I'm moving. It used to be in the root of the domain, now I'm moving it to a directory called "help". I would like to setup a redirect rule that keeps the url path.
Example: I would like
www.domain.net/topic1 to redirect to www.domain.net/help/topic1 etc...
Most solutions I have come up with end up being a redirect loop or do not keep the permalink.

You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^$ help/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!help/).*)$ help/$1 [L,NC]

sadly, if the urls doesn't have anything in common... it seems you'll have to make one rule for each url then add a 302 (temporary) or a 301 (permanent) redirection.
i don't like too much the idea of one line for each url but this could work
Redirect 301 /topic1 /help/topic1
Redirect 301 /topic2 /help/topic2
you could try with something like this
RewriteCond %{REQUEST_URI} ^(topic1|topic2|....|topicN)$ [NC]
RewriteRule (.*) /help/$1 [R=301,L]

Related

htaccess rewrite after redirect

I have a site which uses htaccess to rewrite all pages to the index page with a hash which is then used to serve up content. The file looks like this....
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
I am now moving some of the pages of the site, however if I add a redirect such as....
Redirect 301 /blog /new_location/blog/
I am running into problems with the resulting url looking like
https://mydomain/new_location/blog/urlpath=blog.php
Can anyone suggest a way that I get the page to redirect to mydomain/new_location/blog/ and then run the rewrite on the new url.
Many thanks
RewriteRule and Redirect are from different Apache modules, so run at different times in the processing, not in the order they appear in the configuration. You're best off sticking to one module or the other, by using the [R] flag to RewriteRule.
RewriteRule /blog(.*) /new_location/blog$1 [R=301]
OK, I managed to get this working using a combination of Redirect and Rewrite like so....
Redirect 301 /blog /new_location/blog
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/blog
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
Maybe not the neatest solution, but it work!

url rewrite apache for wildcard domain

I have a rewrite rule for wildcard domains all requests are send to index.php on main domain. This is content for my .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.us$
RewriteRule ^/?$ "http\:\/\/domain\.us\/" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [NC,L]
And it works like it's supposed to work...perfect!
But recently I had to change the url structure of the whole website, that is fine old urls will simple show 404 though few old urls are important to me and I want to redirect them 301 to new url. For example:
somesubdomain.maindomain.com/oldcontent/
I want to reditect
somesubdomain.maindomain.com/importantdir/oldcontent/
Whatever I tried I ended up with a redirection loop (to many redirects at once).
I have like thousands of urls but only want to 301 redirect few of them.

Setting up RewriteEngine and 301 redirect on .htaccess

So I have this media wiki installation and I have since the beginning used rewrite engine on htaccess (as suggested here) to redirect my site, for example (wiki.com) to (wiki.com/wiki/) with the code below.
RewriteEngine On
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/index.php [L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^/?images/thumb/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ %{DOCUMENT_ROOT}/thumb.php?f=$1&width=$2 [L,QSA,B]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^/?images/thumb/archive/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ %{DOCUMENT_ROOT}/thumb.php?f=$1&width=$2&archived=1 [L,QSA,B]
But now I would like to add a separate 301 redirect of a directory from wiki.com/example to wiki.com/wiki/example while still keeping the original wiki rewrite
//301 Redirect Entire Directory
RedirectMatch 301 /example(.*) /wiki/example/$1
The problem is when I add that 301 redirect the whole site just goes kaput, I'm not experienced in the syntax at all so I have no idea how to couple the 2 bits while still keeping the site working.
Hope you can help me out with this. Thanks in advance
Try this:
RedirectMatch 301 ^/example(.*)$ /wiki/example/$1

How to redirect URLs based on query string?

I successfully mass migrated a Wordpress site to Drupal. Unfortunately in Wordpress, the content URL's were something like www.example.org/?p=123. My domain is still the same, but I want to do a redirect via htaccess as Drupal will not allow URL's to be www.example.org/?p=123. In other words, the content does not have the same URL as it did in Wordpress. For example, the new Drupal URL would be something like www.example.org/content/MyNewPage
I tried this in my .htaccess file and it does not work
Redirect 301 /\?p=375 http://www.example.org/content/MyNewPage
So I tried the below, but it does not work either.
Redirect 301 /\?p\=375 http://www.example.org/content/MyNewPage
Just as a test, I tried the below and it worked.
Redirect 301 http://www.example.org http://www.google.com
I made sure that my Redirect rule is at the top of the list in my .htaccess so it will be evaluated first. How do I fix this?
neither Redirect nor RedirectMatch allow you to specify a query string for the redirect source.
[Source]
You have to use mod-rewrite for redirecting based on query string:
RewriteCond %{QUERY_STRING} ^p=375$
RewriteRule (.*) http://www.example.org/content/MyNewPage? [R=301,L]
You may consider use ModRewrite in your htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^p=345$ [NC]
RewriteRule index.php content/MyNewPage [NC,L,R=301]
</IfModule>
And you also may want to pass the old page id to the new URL concatenated (or maybe by QS?):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule index.php content/MyNewPage-%1 [NC,L,R=301]
</IfModule>

Simple htaccess redirection

I have a standard Drupal site and need to add a redirection rule for SEO reasons
I need to redirect /salon/whatever to /salon-locator/whatever
This is what I have at the moment but I seem to get multiple redirects
RewriteEngine on
Redirect permanent /salon/ /salon-locator/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
You need to stick with mod_rewrite here because after mod_alias processes the Redirect directive, the URI continues through the path processing pipeline and mod_rewrite gets a hold of it and mangles it. If you use only mod_rewrite, you can tell the rewrite engine to stop after you've redirected using the L flag. So you want to replace:
Redirect permanent /salon/ /salon-locator/
with this:
RewriteRule ^salon/(.*)$ /salon-locator/$1 [L,R=301]