Forum Moderators: phranque

Message Too Old, No Replies

Rewrite to subdirectory on Drupal-based site

         

Ninja Overlord

8:03 am on Aug 30, 2008 (gmt 0)

10+ Year Member



I am trying to clean up the URLs on my drupal based website.

Right now, there's currently a 301 redirect to the directory example.com/sa - I was wondering if there is a way to do that but not let the end user see the "/sa" in the address - As I am also using a CMS, changing the links will not work for me so easily.

My guess would be (from other posts):

Redirect 301 www.example.com http://www.example.com/sa/
rewriteCond %{THE_REQUEST} ^sa/
rewriteRule .* - [R=404]

[edited by: Ninja_Overlord at 8:06 am (utc) on Aug. 30, 2008]

jdMorgan

2:51 pm on Aug 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The following is an example only; Since you did not specify any criteria for deciding whether to rewrite root URLs to the subdirectory, I assumed that *all* URLs with the exception of those resolving to the customary robots.txt, favicon.ico, privacy-policy, content-rating, and search engine sitemap and authorization files are to be rewritten.

You may or may not have all of these files -- If not, you can omit them from the second and third RewriteConds in the second rule. The first RewriteCond in that rule prevents an infinite rewriting loop.

The first rule-set will externally redirect any direct client requests for pages at /sa subdirectory URLs back to the corresponding page URLs in the root directory. This will clean up your search engine listings faster and preserve old backlinks and bookmarks, but is optional.

The second rule internally rewrites requests for all pages and resources at root directory URLs (except for those explicitly excluded) to the corresponding files in the /sa subdirectory.

In example.com/.htaccess :


Options +FollowSymLinks -MultiViews
RewriteEngine on
#
# Redirect client requests for /sa/ URLs to the Web root directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sa/
RewriteRule ^sa/(.*)$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite root directory requests to /sa subdirectory, except for common root files
RewriteCond $1 !^sa/
RewriteCond $1 !^(robots\.txt¦favicon\.ico¦w3c/p3p\.xml¦labels\.rdf¦privacy-policy\.html)$
RewriteCond $1 !^(sitemap\.xml¦google[0-9a-f]{16}\.html¦y_key_[0-9a-f]{16}\.html¦LiveSearchSiteAuth\.xml)$
RewriteRule (.*) /sa/$1 [L]

Replace all broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Note the use of the terms "external redirect", "internal rewrite", "URL", and "file." To understand this code, it is important to understand that redirects and rewrites are different [webmasterworld.com], and that URLs and filenames are not at all the same thing.

If the on-page links produced by Drupal are page-relative, you may need to change them to server-relative in order for all of this to work -- I'm not that familiar with Drupal and its options, and I'm not sure what "plug-ins" you may be using with it. At any rate, make back-ups of your current .htaccess files before trying this. The code above is fairly "standard," so it'll either work with your set-up or it won't. If it does not work, then you'll need to go to "Plan B."

A bit of friendly advice: Don't "guess" about server configuration directives and code. Read the Apache documentation and try to understand what every directive and every regular-expressions token means. Copying code from forums without understanding how it works and what it does to your server configuration is a recipe for disaster; You can easily knock your server off-line (if you're lucky) or create a 'silent' problem that destroys your search engine rankings if the code is not exactly right --character-for-character-- for your site and server set-up. Links to useful documentation and resources are available in our Apache Forum Charter [webmasterworld.com].

Jim