Redirecting members with htaccess - .htaccess

I need to redirect members to a different URL.
From: https://domain.com/member/memberID
To: https://domain.com/members/memberID
So, everything after members/ should stay the same (logged user ID), the only change is an "s" in member"s"..
What's the best way to do this?

To redirect the contents of a whole directory to another use the below:
RewriteRule ^member/(.*)$ /members/$1 [R=301,NC,L]

Related

htaccess 301 redirect all website pages with query to /

In the past website was full of pages with different basis. After redisign and with new concept the website consists of only 1 page. So I need to redirect all old pages to /.
Pages for example:
http://domain.com/catalog/index.html?c_id=145
http://domain.com/catalog/?c_id=116
http://domain.com/news/read.html?id=174
and so on.
In htaccess I've made several rules like this:
RewriteRule ^catalog(.*)$ http://domain.com/ [L,R=301]
But this rule don't give a full control, and the result is not satisfactory:
http://domain.com/?c_id=145
How can I get rid of this? I need to redirect all such links to http://domain.com without any additions. I think the {QUERY_STRING} can help, but I don't know how to do this correct.
Any help would be appreciated. Thanks!
You can add a ? at the end to create a blank query string:
# append "?" here ----v
RewriteRule ^catalog(.*)$ http://domain.com/? [L,R=301]

htaccess help to rename directory

I have 2 directories that I would like to rename without losing my search ranking. Here is the url convention and suggested name change:
Old: mysitename.com/folder1/folder2/filenames
to
New: mysitename.com/newfoldername1/newfoldername2/filenames
filename is the given name of each page
RewriteRule ^(.*)/folder1/folder2/(.*)$ $1/newfoldername1/newfoldername2/$2 [R,L]
The problem with this rule is that RewriteRules only match on the path, not the domain. You have the (.*)/ at the front of the rule as if you are trying to match the domain, which isn't necessary. You have also set it as a redirect rather than a rewrite ... maybe this is what you want, in which case keep the "R" flag as you had it, but I'll remove it in the code below in case you just want to rewrite. Modify the rule as follows and it should work:
RewriteRule ^newfoldername1/newfoldername2/(.*)$ /folder1/folder2/$1
Edit: Now that you've posted your other rules, I removed the [L] and reversed the order to what I think you're trying to do.
Edit 2: The above rule assumed that you wanted a user entering domain.com/newfoldername1/newfoldername2/whatever to be silently rewritten to domain.com/folder1/folder2/whatever. The phrasing of your latest comment indicates that instead you want a user entering domain.com/folder1/folder2/whatever to be redirected (in other words, to have the address change in their browser) to domain.com/newfoldername1/newfoldername2/whatever and that the server is ready to process this new path. In that case, the following rule is required:
RewriteRule ^folder1/folder2(/.*)?$ /newfoldername1/newfoldername2$1 [R=301,L]

I want to be able change the root directory using htaccess

I want to be able change the root directory using htaccess.
I want this:
index.php/user/user_postdetail/index/14
To become this:
index.php/post_14.html
I tried using
RewriteRule post_([0-9]+).html$ /index.php/user/user_postdetail/index/$1 [L=301,R]
but I am getting it reverse. Any ideas?
You have a typo. It should be R=301 as it stands for Redirect with 301 code, whereas L means Last rule to call.
Additionally, you do have it backwards.
What you need is:
RewriteRule user_postdetail/index/([0-9]+)$ /index.php/post_$1 [R=301,L]

How to 301 redirect pages "up" a page

I'm a newbie and I'm trying to figure out the proper 301 redirect for the following pages. I hope I'm being clear here :) In my .htaccess file, I want to redirect pages "up" one pages without having to do every page individually.
My original pages looked like the following:
www.doctors.com/skin/california/best-skin-doctors-california/
www.doctors.com/skin/california/best-skin-doctors-california/?page=1
www.doctors.com/skin/california/best-skin-doctors-california/?page=2
....etc. ....up to like /?page=33
and more categories and states, like:
www.doctors.com/heart/new-york/best-heart-doctors-new-york/
www.doctors.com/heart/new-york/best-heart-doctors-new-york/?page=1
www.doctors.com/heart/new-york/best-heart-doctors-new-york/?page=2
...etc. .....again up to like /?page=24
I've since changed the page structure to eliminate the long URLs...like this:
www.doctors.com/skin/california/
www.doctors.com/skin/california/?page=1
www.doctors.com/skin/california/?page=2
etc.....and similarly....
www.doctors.com/heart/new-york/
www.doctors.com/heart/new-york/?page=1
www.doctors.com/heart/new-york/?page=2
etc.
How can I "bulk" redirect the original pages with the long URLs to the newer, shortened version in my .htaccess file? Thank you very much for your time and consideration!
Using mod_alias, you can simply add this in the .htaccess file in your document root:
RedirectMatch 301 ^/([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/
But if you need further restrictions on how the redirect works, you can use Apache's mod_rewrite module. Taking a look at the RewriteCond directive, you can impose conditions on a rule and put everything in .htaccess. The main rule will look very similar to mod_alias' RedirectMatch. Example:
RewriteRule ^([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/ [R=301,L]
In both cases, the query string (the page=3 part) is simply appended to the new target. Looking over the different things you can do with RewriteCond, say if you wanted to exclude this rule when requests are made for something like /images/ or /themes/:
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/themes/
RewriteRule ^([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/ [R=301,L]
So, if the request doesn't start with /images/ and the request doesn't start with /themes/, then apply the rule. This example would make it so a request for http://host.com/themes/subSilver/magic-icons/ don't get redirected to http://host.com/themes/subSilver/.

Replace directory name in url with another name

Using htaccess, how can I change the url "http://www.website.com/abc/..." to "http://www.website.com/xyz/..." so it acts as a redirect to the same filename/directory structure after "xyz" as it did after "abc".
RewriteRule ^abc/(.*)$ /xyz/$1 [R=302,L,QSA]
This will redirect example.com/abc/pink-kitten to example.com/xyz/pink-kitten (or example.com/abc/ to example.com/xyz/) but will not do anything if trailing slash after the folder name is missing (i.e. example.com/abc will not be redirected). If you need the last case as well then you will need to use separate rule for that.
You can change redirect code [R=302] to another if required (e.g. 301 Permanent Redirect).

Resources