Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you simply add new custom rewrites to the .htaccess of a wordpress without fail?

I've added some extra functionality to my wordpress so that I can visit it with a variable and do extra stuff.

The problem is, when I turn my ugly dynamic link into lovely permlink formatting in the .htaccess file, wordpress overrides it / ignores it. I've heard there's a way to do it, but the ways I try to do it based off what people have said still returns a 404 page regardless. I know that the file its pointing to works.

2 ways ppl say works but I've had no joy with:

1) insert the rules above the #BEGIN wordpress part 2) use add_rewrite_rule() wordpress function somewhere

Has anybody had any success with these methods? or other methods?

Here's what my .htaccess file looks like

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/ref/(.*)$ /index.php?ref=1&sid=$1 [NC]
</IfModule>


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


</IfModule>

In my themes function.php I've also tried adding:

add_rewrite_rule('/ref/(.*)$', 'index.php?ref=1&sid=$matches[1]','top');

With no success.

I've also tried the solutions over @ WordPress + mod_rewrite with no joy.

Please help! :)

any ideas?

like image 302
willdanceforfun Avatar asked Oct 15 '25 09:10

willdanceforfun


2 Answers

This works - I just tested it - Note I added an L to the end of the RewriteRule

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^/ref/(.*)$ /index.php?ref=1&sid=$1 [NC,L]

#wp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
like image 130
Chris Avatar answered Oct 16 '25 23:10

Chris


a new rule will always override the old ones

try the following

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

RewriteRule ^/ref/(.*)$ /index.php?ref=1&sid=$1 [NC]


</IfModule>
like image 25
perrohunter Avatar answered Oct 16 '25 22:10

perrohunter