WordPress.org

Ready to get started?Download WordPress

Forums

.htaccess - Is WP Killing My Rules? (7 posts)

  1. redwallhp
    Member
    Posted 6 years ago #

    I'm writing a plugin currently, and I'm needing to add a rule to my .htaccess file to complete its functionality. I'm trying to redirect requests from /go/alphanumericstring to /index.php?gocode=alphanumericstring. I've been trying to get my rewrite rule working for a couple hours now, but it's still not working. Here's my .htaccess file:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /sites/test-wp/
    RewriteRule ^go/([A-Za-z0-9]+)/?$ /index.php?gocode=$1
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /sites/test-wp/index.php [L]
    </IfModule>
    
    # END WordPress

    The bold line is the one that I am having trouble with. I've tried several iterations of it, moved it to different locations in the file, and even put it above the "Start WordPress" line (with a RewriteEngine On line, of course). Nothing is working.

    Does anyone have a solution?

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

    If you put rules inside the # BEGIN WordPress and # END WordPress lines, then yes, WordPress will remove them from time to time.

    Don't put your rules inside those lines. Make your own rewrite section above the WordPress one and put your rule there instead.

  3. redwallhp
    Member
    Posted 6 years ago #

    I already tried that. WordPress isn't removing the rules, but they're just not working.

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

    Ah. Well, in particular that "go" RewriteRule you have won't work because you don't have an [L] at the end of it.

  5. redwallhp
    Member
    Posted 6 years ago #

    Well, there's some progress. The current iteration looks like this:

    RewriteEngine On
    RewriteBase /sites/test-wp/
    RewriteRule ^go/([A-Za-z0-9]+)/?$ /index.php?gocode=$1 [L]
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /sites/test-wp/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /sites/test-wp/index.php [L]
    </IfModule>
    
    # END WordPress

    For some reason it's bouncing requests to ntugo.com/index.php?gocode=$1 instead of ntugo.com/sites/test-wp/index.php?gocode=$1

    Why's that?

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

    For some reason it's bouncing requests to ntugo.com/index.php?gocode=$1 instead of ntugo.com/sites/test-wp/index.php?gocode=$1

    Why's that?

    Err... Because that's what you told it to do?

    RewriteRule ^go/([A-Za-z0-9]+)/?$ /index.php?gocode=$1 [L]

    Right there, you told it to go to /index.php. If you want it to go to /sites/test-wp/index.php, then your rule should have been:

    RewriteRule ^go/([A-Za-z0-9]+)/?$ /sites/test-wp/index.php?gocode=$1 [L]

  7. redwallhp
    Member
    Posted 6 years ago #

    Ah, thanks. Everything is working fine now.

Topic Closed

This topic has been closed to new replies.

About this Topic