WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] mod_rewrite: remove string (4 posts)

  1. Sebastian
    Member
    Posted 6 years ago #

    Hi folks,

    I'm currently migrating my blog to wordpress. Seems to work just fine, but I also want to migrate some of my old permalinks, because there are some links in the net pointing at them. Basically, it should be possible to do with mod_rewrite, but I can't get it working.

    Example of old permalink:

    http://domain/blog/fixedstring/variablestring/2007/05/01/entry

    Same post with new permalink:

    http://domain/2007/05/01/entry

    So basically I have to remove "blog/fixedstring/variablestring/". I would write the following rule:

    (^blog/fixedstring/.[a-z]*/)(.*) $2

    This works, if I use it outside of wordpress, but I don't know how to integrate it with the standard wordpress .htaccess file. I thought it must work as follows:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule (^blog/fixedstring/.[a-z]*/)(.*) $2
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress

    Does anybody knows what is going wrong?

    Regards,

    Sebastian

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Try this instead:

    RewriteRule (^blog/fixedstring/.[a-z]*/)(.*) $2 [R=301,L]
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress

    You really never want to change WordPress's rules directly. Consider the text between BEGIN and END to be inviolate unless you really, really know what you're doing. ;)

  3. Sebastian
    Member
    Posted 6 years ago #

    I think the [R=301,L] at the end did the trick, because I already tried it with out it. So what does it actually do? Can I use several rules, for example to rewrite the old feed URL?

    Sebastian

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    R=301 causes it to send a redirect to the browser, changing their URL. The reason for the 301 is to say that it's a Permanent Redirect. This allows search engines to recognize that your URL has changed and update themselves accordingly. Without the =301 it would send a 302 (by default) and that's a Temporary Redirect.

    The L makes it the Last rule in the processing chain. Once it hits that, it stops processing. For a redirect, you almost always want an L in there to stop the server processing and immediately force the user to the new URL.

    You may want to read the documentation for mod_rewrite.

Topic Closed

This topic has been closed to new replies.

About this Topic