Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add URL Rewrite Rule To Wordpress

I've written a Wordpress plugin that adds a query string to the URL. However I can't seem to modify the htaccess to rewrite this. Not sure if Wordpress is overriding it?

The current htaccess is:

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

And the URL I'm trying to rewrite is:

http://domain.com/deal-info/?id=87&post_name=testdealtitle

Desired URL:

http://domain.com/deal-info/87/testdealtitle

I've tried adding:

RewriteRule ^([^/]*)/([^/]*)$ /deal-info/?id=$1&post_name=$2 [L]

to no avail. Any ideas appreciated!

like image 737
logic-unit Avatar asked Dec 12 '11 15:12

logic-unit


People also ask

How do I rewrite a URL in WordPress?

You can simply rewrite the URL in your WordPress website by using the . htaccess file. Any changes that you will make in the permalink will change your Website's . htaccess file and will send your traffic to your index.

Where do you put rewrite rules?

A rewrite rule can be invoked in httpd. conf or in . htaccess . The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.


Video Answer


2 Answers

When you do this:

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

This means: "if it's not a file, and if it's not a dir, redirect all incoming URLs to index.php.

Your rewriterule should work... unless you've put it after the previous rewriterule. So, in short, are you sure your .htaccess it like that:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([^/]*)/([^/]*)$ /deal-info/?id=$1&post_name=$2 [L]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

Please tell me if it works.


Moreover if you only want to redirect only deal-info, you should do something like (not tested):

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(/)?deal-info/([^/]*)/([^/]*)$ /deal-info/?id=$2&post_name=$3 [L]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

Two hints:


Please try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

May I ask you to add the rewritelog in your question?

like image 157
Olivier Pons Avatar answered Oct 03 '22 19:10

Olivier Pons


After much confusion I found an answer here: https://wordpress.stackexchange.com/questions/5413/need-help-with-add-rewrite-rule

Though I think the proper way to do it is by: http://codex.wordpress.org/Rewrite_API/add_rewrite_rule

I'm not sure if you can do this directly in htaccess, or I was having a conflict problem with another plugin I was using.

Thanks again to Olivier for his extensive answer!

like image 23
logic-unit Avatar answered Oct 03 '22 20:10

logic-unit