Replace directory name in url with another name - .htaccess

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).

Related

How can i 301 redirect a subfolder and everything after it?

I am trying to redirect a subfolder as well as anything after it to the home page.
For example:
example.com/subfolder/extra-stuff > example.com
The extra-stuff is constantly changing and auto generated, so I want the redirect to remove that as well.
I am using:
Redirect 301 /subfolder(.*) http://www.example.com
However, this will result in http://www.example.com/extra-stuff.
Is there a way I can say if /subfolder(and anything else after subfolder) redirect to home?
Thanks for any suggestions!
The Redirect directive uses simple prefix-matching and everything after the match is copied onto the end of the target URL (which is what you are seeing here). However, the Redirect directive also does not support regex syntax, so a "pattern" like (.*) on the end will actually match the literal characters (, ., * and ) - which shouldn't have worked in your example?!
You'll need to use RedirectMatch instead (also part of mod_alias), which does use regex, and is not prefix matching.
For example:
RedirectMatch 301 ^/subfolder http://www.example.com/
Any request that starts /subfolder will be redirected to http://www.example.com/ exactly.
You'll need to clear your browser catch before testing.
You tagged your question "Magento" (which is probably using mod_rewrite). You should note, however, if you are already using mod_rewrite for rewrites/redirects then you should probably be using mod_rewrite instead of mod_alias to do this redirect, since you can potentially get conflicts.
For example, the equivalent mod_rewrite directive would be:
RewriteRule ^subfolder http://www.example.com/ [R=301,L]
Note there is no slash prefix on the RewriteRule pattern. This would need to go near the top of your .htaccess file.

mod_rewrite for a 301 redirect. Not redirecting to proper location

I'm trying to use a RewriteRule (using ISAPI, NOT on an Apache server) to 301 redirect a url such as:
http://www.mydomain.com/news/story-title/
to
http://www.mydomain.com/news/detail/story-title/
What I've gotten so far is:
RewriteRule ^news/(?!detail)/?$ news/detail/$1/ [L,R=301]
which successfully ignores urls that already have the "detail" in them (in some of my first attempts I ended up with a loop and a url like "/news/detail/detail/detail..."), but visiting /news/story-title/ gives me a 404 so it's not redirecting to the proper location.
Change your rewrite rule to
RewriteRule ^news/(?!detail)([^/]+)/?$ news/detail/$1/ [L,R=301]
EDIT : (How it works?)
/(?!detail) is a negative lookahead but it's also non-capturing i.e. it matches / but not what comes after it; just makes sure that it isn't "detail". So, I added a capturing group ([^/]+) to capure those characters (one or more + of anything that's not a/) optionally ending with a /.
Hence, the $1 now gets replaced with the matched directory name.

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]

Is it possible to remove a "folder" at the end of a URL using an .htaccess file?

so I have a bunch of URLs
http://foo.com/people/smith/john/1
http://foo.com/people/cartman/eric/2
http://foo.com/people/simpson/bart/3
I want to change the URLs like so
http://foo.com/people/smith/john/
http://foo.com/people/cartman/eric/
http://foo.com/people/simpson/bart/
So basically, I think I need to write a condition that looks for the folder 'people' and removes the third directory from the URL. Is this possible with my .htaccess?
In the htaccess file in your document root, try:
RewriteEngine On
RewriteRule ^people/([^/]+)/([^/]+)/.+ /people/$1/$2 [L,R=301]
This redirects the browser (changing the URL in the address bar) from
http://foo.com/people/smith/john/1
to
http://foo.com/people/smith/john/
If you don't want the URL in the browser's address bar to change, remove the ,R=301 from the square brackets.

.htaccess 301 Redirect Appending Query's to End of URL

I am hoping someone can help with an unusual situation.
I have one main rewrite rule in place in my httpd.conf file which handles all of our dynamic content. The rule looks like this and works fine:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
The problem I have is that when I try to use .htaccess to create a simple 301 redirect, the query parameters are automatically appended to the end of the URL's so the final result looks like this:
http://www.example.com/category/page.html?&a=/category/subcategory/something/d/page/
Notice that the query string is appended to the URL when using .htaccess to create a 301 redirect.
I have solution for this on a case-by-case basis, but it's not practical to create a new rule each time I want to do a simple 301 redirect.
So, I am wondering if I can edit my "main rule" in any way so that when .htaccess is used to create redirects, the query parameters are not appended to the target URL.
Thanks in advance for any help you can provide.
If you have multiple simple redirects for which you want to suppress query string values you could put all the redirects in a RewriteMap (since you already have access to httpd.conf), and have one .htaccess rule that suppresses the query strings as below
place in htaccess
#if there is a match in the map
RewriteCond ${redirect_map:$1} !=""
RewriteRule ^(.*)$ ${redirect_map:$1}? [R,L]
place in httpd.conf
RewriteEngine On
RewriteMap redirect_map txt:/usr/local/apache/conf/redirect.map
contents of /usr/local/apache/conf/redirect.map
key followed by a space followed by target
directory/subdirectory1/subdirectory2/ example/category7/subdirectory/file.html
directory4/subdirectory2/subdirectory9/ example/category5/subdirectory4/file332.html
That's what your rule has defined it to do:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
It says to create a URL that will look like:
category/refine.cgi?&a=/foo/bar/
If you don't want that to happen, change your rule to be:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?

Resources