Welcome to WebmasterWorld Guest from 54.159.179.132

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

URL redirect in .htaccess?

Redirecting a subfolder to a different subfolder

   
4:18 am on Sep 27, 2013 (gmt 0)



I want users who are searching for www.example.com/support/* to be directed to www.example.com/forums/

But I also want any url with /support/ to redirect.

Example:

www.example.com/support/forum/this-game-is-great should redirect to www.example.com/forums/this-game-is-great

www.example.com/support/how-do-i-login should redirect to www.example.com/forums/how-do-i-login

www.example.com/support/general-forum/what-events-are-happening-today should redirect to www.example.com/forums/what-events-are-happening-today

I just want to redirect anything with "support" in it to go to "forums"

I've got /forums/ right underneath the root folder so the path looks like this: root/forums/

The .htaccess file is inside the /forums folder. Should it be in that subfolder or directly within the root directory?

Here is the code I am using for the redirect:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /forums/

# Redirect old forum path to new forum path
RewriteRule ^support/(.*)$ http://www.example.com/forums/$1 [R=301,L]

No, my website is not "example.com" It's just a placeholder. But I'm wondering if that is the right code to use? and is the file located in the right place on the server?

[edited by: phranque at 5:18 am (utc) on Sep 27, 2013]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

8:30 am on Sep 27, 2013 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, compwiz02!


you're going to need something in the Pattern for the possible subdirectory.
otherwise http://www.example.com/support/general-forum/what-events-are-happening-today will redirect to http://www.example.com/forums/general-forum/what-events-are-happening-today.

the precise regular expression depends on whether or not you have urls requested such as http://www.example.com/support/general-forum/
or http://www.example.com/support/general-forum/subforum/forum-content

there's also the possible uniqueness problem as you're essentially redirecting the contents of a directory and several subdirectories into one directory.
11:46 am on Sep 27, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



The .htaccess file is inside the /forums folder. Should it be in that subfolder or directly within the root directory?

You can have more than one htaccess for minor things. But having RewriteRules in more than one place is a recipe for disaster. Put everything in the root.

In mod_rewrite within htaccess

RewriteRule ^something-here

the opening anchor ^ in the pattern refers to the directory you are currently in. But a leading / in the target always means the root, no matter where you are.

A RewriteBase is only used when the target doesn't have a leading slash and doesn't start with full protocol-plus domain. In other words, never. So you don't need the line at all.

I just want to redirect anything with "support" in it to go to "forums"

In your examples, you've got potential subdirectories within /support/. How many different directories are there? I assume they're locations that used to exist, so there can't be a vast number of them. In fact there can't be all that many filenames, total.

^support/(?:(?:onedirectory|otherdir|thirddir)/)?(this|that|the-other)
or
^support/(?:\w+/)?(more-stuff-here)
and at worst
^support/(?:[^/]+/)?(.+)

If you use ?: for non-capturing groups, you don't have to keep track of the parentheses.

:: hasty detour to switch off smileys after seeing what happens to the sequence (?:(?:o ::
12:09 pm on Sep 27, 2013 (gmt 0)



No subdirectories. "support" IS the actual subdirectory. All of the folders below it such as /forums/admincp and /forums/images are folders supporting it.

In my examplee where I have multiple subfolders like /support/general-forum/, "general-forum" is not a sub-folder....it's a sub-forum created in the database. On my forum, everything under /support (or /forums) is a forum category created in the database. /support is the actual folder in the root directory which I want to direct to /forums.

I don't have a .htaccess in any other place. the /forums/.htaccess is the only location with a .htaccess file.

So essentially, I want the redirection to take any url with "/support" in it and change it to "/forums"

/support --> /forums
/support/general --> /forums/general
2:46 pm on Sep 27, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



www.example.com/support/
forum
/this-game-is-great

should redirect to
www.example.com/forums/this-game-is-great


www.example.com/support/how-do-i-login

should redirect to
www.example.com/forums/how-do-i-login


www.example.com/support/
general-forum
/what-events-are-happening-today

should redirect to
www.example.com/forums/what-events-are-happening-today


According to your example, this is not a straight "should go to" procedure.

In two of the three examples, the additional intermediate folder levels seen in the original request are not found in the new URL.

Please confirm the above examples are correct before proceeding.
8:09 pm on Sep 27, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



No subdirectories. "support" IS the actual subdirectory. All of the folders below it such as /forums/admincp and /forums/images are folders supporting it.


www.example.com/support/forum/this-game-is-great should redirect to www.example.com/forums/this-game-is-great

:: sigh ::

Is it a language problem?
2:58 am on Sep 28, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



The .htaccess file is inside the /forums folder. Should it be in that subfolder or directly within the root directory?

Within the root is almost always best.

Here is the code I am using for the redirect:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /forums/

# Redirect old forum path to new forum path
RewriteRule ^support/(.*)$ http://www.example.com/forums/$1 [R=301,L]

Options +FollowSymLinks
RewriteEngine on

#
# Forget the base, just use a sever relative path instead if you're rewriting
# Meaning Use: /the-full-path/to-the/file.ext
# The base should not matter for redirecting.
#

RewriteRule ^support/(.*)$ http://www.example.com/forums/$1 [R=301,L]

www.example.com/support/general-forum/what-events-are-happening-today
should redirect to
www.example.com/forums/what-events-are-happening-today

This one won't work with the above. You'll need a second rule that comes before the preceding to redirect support/general-forum to /forums/ or a conditional version of the rule.

# 2nd Rule Version
RewriteRule ^support/general-forum/(.*)$ http://www.example.com/forums/$1 [R=301,L]
RewriteRule ^support/(.*)$ http://www.example.com/forums/$1 [R=301,L]

# Conditional Version [preferred]
RewriteRule ^support/(general-forum/)?(.*)$ http://www.example.com/forums/$2 [R=301,L]
3:58 am on Sep 28, 2013 (gmt 0)



I'm using vBulletin 5...according to what some senior members are saying, the RewriteBase is required...*shrugs*

[vbulletin.com...] - Post #6

Also, [kb.mediatemple.net...] (the example below is kinda what I'm trying to achieve....folder1 is /support and folder2 is /forums)

This .htaccess file will redirect http://example.com/folder1/file.html to http://example.com/folder2/file.html. Choose this version if your content is duplicated in both directories:
File name: .htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1/(.*)$ [gs.mt-example.com...] [R=301,L]


Is htaccess able to find a specific text in a URL, change it to the new text that it will redirect to ("support" to "forums") and then carry out the rest of the URL? If there's more text after "/support/", can htaccess hook the text to the new text "/forums/"? Or do I have to create a specific rule for every URL that has a category after /support/?

Eh man.....trying to make this as clear as possible to you guys...
8:52 am on Sep 28, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



RegEx works with "patterns".

It can very easily change
example.com/this/stuff to example.com/that/stuff and
example.com/this/other/stuff to example.com/that/stuff
9:15 am on Sep 28, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Is htaccess able

htaccess is simply a location. The redirecting is done by mod_rewrite. Your RewriteRules happen to live in an htaccess file; someone else could have the (almost-)identical rules in a server config file.

This .htaccess file will redirect

Eeuw. That's quoting someone else, right? When you are dealing with Regular Expressions, you have to be very very precise. Especially if the Regular Expressions are happening within mod_rewrite.
2:22 am on Sep 29, 2013 (gmt 0)



I've moved the .htaccess file from /forums/ to the root. So now it's /.htaccess instead of /forums/.htaccess. I've also tweaked the code so that any query that is entered after "support" will be put after "forums" (hopefully).

# Redirect old forum path to new forum path
RewriteCond %{QUERY_STRING} ^support/(.*)$
RewriteRule ^support$ http://www.luviniaworld.com/forums/%2


The entire code for this area of the file is this:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /forums/
# Send css calls directly to the correct file VBV-7807
RewriteRule ^css.php$ core/css.php [NC,L]

# Redirect old install path to core.
RewriteRule ^install/ core/install/ [NC,L]

# Redirect old forum path to new forum path
RewriteCond %{QUERY_STRING} ^support/(.*)$
RewriteRule ^support$ http://www.mywebsite.com/forums/%2


# Main Redirect
RewriteCond %{REQUEST_URI} !\.(gif|jpg|jpeg|png|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?routestring=$1 [L,QSA]

# Because admincp is an actual directory.
RewriteRule ^(admincp/)$ index.php?routestring=$1 [L,QSA]

</IfModule>


SO it's got the same effect as this:

# Redirect old forum path to new forum path 
RewriteRule ^support/(.*)$ http://www.example.com/forums/$1 [R=301,L]


I can type /support on my website and it will put "support" after the redirect like this: mywebsite.com/forums/Support

But if I put /support/index.php, it will lead me to a forum page stating "invalid page".

So yea, still need help...
2:23 am on Sep 29, 2013 (gmt 0)



by the way, mywebsite.com and example.com are placeholders for my actual website used in the code.
6:55 am on Sep 29, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



If you need to name more than one domain, you can say example.org, example.net, example.nz, example.co.uk ... or, for that matter,
http://www.example.old
http://www.example.new
if that makes it easier. The magic word is "example".

But if I put /support/index.php, it will lead me to a forum page stating "invalid page".

Isn't that what you wanted to do? Redirect absolutely all requests for /support/ to /forums/ ?

If requests for /forums/ are plugged into further RewriteRules, you have to make sure they don't conflict. But that's not the /support/ redirect's fault.

RewriteRule ^(admincp/)$ index.php?routestring=$1 [L,QSA]

This one, specific, single, individual page? Then why does its name have to be captured?
1:42 pm on Sep 29, 2013 (gmt 0)



Isn't that what you wanted to do? Redirect absolutely all requests for /support/ to /forums/ ?


Yes

In my root htaccess file, I have this:

Options +FollowSymLinks 
RewriteEngine on

RewriteCond %{QUERY_STRING} ^support/(.*)$
RewriteRule ^support$ http://www.example.com/forums/%1 [L,QSA]


Anything after "support" will transfer to after "forums".

Example:

example.com/support/anything-after

redirects to

example.com/forums/anything-after

As of right now, it's not having any effect. /support isn't redirecting....just giving me a "Forbidden You don't have permission to access /support/ on this server." error
4:50 pm on Sep 29, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



There's no query string in your example URLs, but there's a query string test in your rule.

Do your URLs really look like
example.com/support?support/<something>
?
8:17 pm on Sep 29, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



But if I put /support/index.php, it will lead me to a forum page stating "invalid page".

This is confusing. Never mind what the screen says. What is the URL at this point? When you say "a forum page" it implies that you've ended up in the /forums/ software.

/support isn't redirecting....just giving me a "Forbidden You don't have permission to access /support/ on this server." error

Where does the 403 come from? Is it the ordinary human lockout because there's a /support/ directory but it has no named /index file? Or have you inadvertently locked yourself out for other reasons?

Unfortunately logs are not helpful about reasons for a 403; all it ever says is "request denied by server configuration". (Thanks, logs, that much I could have worked out on my own.) But you still need to start looking at your raw logs so you can see what requests are coming in and what happens to them.

If you install a pseudo-server like MAMP or WAMP you can also run a RewriteLock. But it creates a pretty big haystack, generally not all that useful.
1:15 am on Sep 30, 2013 (gmt 0)



This is what the URL looks like at the moment:

http://www.example.com/forums/

Here are three forum categories seen on my forums:

http://www.example.com/forums/forum/official-news-and-announcements/in-game-events

http://www.example.com/forums/forum/main-category/activities

http://www.example.com/forums/forum/main-category/weddings

I'm using vBulletin 5.0.4 software so the url structure is /forums/forum. It repeats itself. I don't know why. If you go to the official vBulletin forum, you will see the same URL structure.

My OLD URL looked like this:

http://www.example.com/support/

The folder, /support, still exists in the root directory but half the files are missing.

There....I've provided REAL examples of URLs on my forum. Now can someone help me?
1:19 am on Sep 30, 2013 (gmt 0)



I want the redirect code to take the old URL with "/support/" in it, change it to "/forums/", then insert everything that came after "/support/" and put it after "/forums/"

example:

http://www.example.com/support/forum/main-category/weddings

Change "/support/" to "/forums/"

http://www.example.com/forums/

Take "forum/main-category/weddings" and put it after "/forums/"

http://www.example.com/forums/forum/main-category/weddings

Can this be possible?
3:55 am on Sep 30, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



I think you can now go back to the first two-three posts in this thread and disregard what came later. The confusion arose because you really do have "/forums/forum" like that.

RewriteRule ^support/(.*)$ http://www.example.com/forums/$1 [R=301,L] 


This is the code that should have worked and I think it's what you had all along. So if the redirect isn't taking place, there's some other and unrelated reason.
4:47 am on Sep 30, 2013 (gmt 0)



Do I need to use <IfModule mod_rewrite.c> </IfModule>?
8:05 am on Sep 30, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



No, never. Envelopes in the form <IfModule> are a feature of boilerplate htaccess files made for generic distribution. Once it has arrived in your personal web space, get rid of all these envelopes. Either you have a particular module or you don't. (And if you don't, you wouldn't be using the CMS that came with this htaccess in the first place :))

Technically you need
Options +FollowSymLinks
but in practice any host that allows htaccess will already have this enabled in the config file.

You do absolutely, unequivocally need
RewriteEngine On
Without this line, no RewriteRules will be recognized.
12:45 am on Oct 1, 2013 (gmt 0)



This is the code that ended up working (so far, no problems):

Options +FollowSymLinks 
RewriteEngine on

RewriteRule ^support/(.*)$ http://www.example.com/forums/$1 [R=301,L]


Came to realize there was a config and an htaccess files in the /support folder so I renamed both files and the redirect started working.

[edited by: phranque at 5:07 am (utc) on Oct 1, 2013]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

 

Featured Threads

Hot Threads This Week

Hot Threads This Month