tags:

views:

137

answers:

2

I'm finding a lot about creating pretty URIs and whatnot using mod_rewrite but what I'm looking for is a way to remove elements of a URL (as far as the viewer is concerned) and not just moving stuff around and/or reorganizing.

I need this:

http://www.mysite.com/foo/bar

to be this:

http://www.mysite.com/bar

Basically, let's get rid of /foo in the URL.

Is that possible?

+1  A: 

You can easily achieve that by simply dropping what you want to lose:

RewriteEngine On
RewriteRule ^foo/(.*)$  $1  [R=301,NE,L]

That should effectively remove foo/ from your URL. If you don't want the address bar to reflect this change, remove R=301. The NE parameter is there to make sure that the request is not escaped.

Andrew Moore
Doesn't this do the exact opposite of what he wants? He wants the URL to be example.com/bar, and refer to the file at /foo/bar. This rewrite rule seems to turn example.com/foo/bar into the file at /bar. Or maybe I just don't know enough about mod_rewrite?
Nick Lewis
If he wants to refer to /bar as /foo/bar, then he would need a rule for each /foo he wanted to reference
Chacha102
Hmm... so something like:RewriteRule (these|are|the|dirs|I|want|to|filter|out)/(.*)$ $1 [R=301,NE,L]
J. LaRosee
@Andrew: This is what I've got now, modified w/ your suggestion. Still doesn't want to work. You might see why where I don't:<IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /blog2/RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /blog2/index.phpRewriteRule ^blog2/(.*)$ /$1 [R=301,NE,L]</IfModule>
J. LaRosee
Remove your `RewriteBase` statement.
Andrew Moore
A: 

NOTE: Most of these instructions came from here, "Giving Wordpress its Own Directory".

If it is Wordpress specifically, this can be handled by adding an index.php that looks like this to the directory you want to be the base:

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require('./blog2/wp-blog-header.php');
?>

In Wordpress settings, you need to let it know the difference between the "actual" Wordpress URL and the one you want everyone else to use:

(These were under "Settings" for me...)

Also, I cannot remember if this is done automatically or not (I thought it was), but the .htaccess where you put the index.php file will need to look like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
Beau Simensen
I guess this was more a questions about WP rather than mod_rewrite.
J. LaRosee