Add URL Rewrite Rule To Wordpress - 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!

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?

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!

Related

Wordpress URL - Need to remove a GET parameter

I know there are a lot of threads for .htaccess URL rewriting, but my case seems to be a bit different and I have tried a lot but it doesn't work.
My current URL: http://example.com/forest/trees/?type=perennial
What I need is: http://example.com/forest/trees/perennial
I just need to remove the ?type= from the URL.
EDIT: The URL may contain hyphens - between strings at any point (except the domain name ofcourse). It can be dense-forest or non-perennial too.
It's a custom code and plugin, so can't modify it. I just need the URL beautified.
Any help would be greatly appreciated.
What I've tried so far in .htaccess:
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /forest\/trees\/\?type=([^&]+)
RewriteRule ^ \/forest\/trees\/%2\/? [L,R=301]
and
RewriteRule ^\/forest\/trees\/([^/]*)? /forest/trees/?type=$1 [L]
My current Wordpress .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
Thanks in advance. :)
Cheers!
You can use this .htaccess file:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^forest/trees/([^/]+)$ /forest/trees/?type=$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Now URLs like http://example.com/forest/trees/perennial will internally redirect to /forest/trees/?type=perennial and then to Wordpress Dispatcher /index.php.

How to clean url wordpress

I'm using wordpress for a site,
this url before tes.com/index.php/about/
i want url like this tes.com/about/
I have tried copying parts of WordPress' .htaccess, however shows a 404 error.
My .htaccess file looks like:
# 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
Any ideas on where to start and how to accomplish what I'm looking for?
Go to your WP-ADMIN-->Settings-->Permalink and use the permalink structure change there, if it generate any .htaccess file copy the content and update your .htaccess file.
Or Check if your hosting mod_rewrite is enable by creating a file phpinfo.php with content,
Upload this file and browse via Browser. So you know which modules are enabled. You need mod_rewrite enable to remove index.php from URL.
Try this -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test.com/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test.com/index.php [L]
</IfModule>
The official instructions will give you clean urls. So best to follow this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

htaccess rewriting user pretty url to perform query

Say I have a site mysite.com, which is a wordpress site.
I already have the following rewrite rules to remove index.php:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
if I go to mysite.com/?blog_type=a-blog-type I get the proper page with only the blogs of that type
What I would like, is for the user to be able to type in mysite.com/blogs/a-blog-type/ and have that acutally do the query, but keep on displaying /blogs/a-blog-type/
I've looked at many similar answers on here, but most seem to make it so that it just redirects from the query, to the pretty url, which I don't want, as the pretty url just leads to a 404 error as it's not doing the query.
Thanks for the help.
This rewrite rule will take any mysite.com/blogs/a-blog-type/ URL and rewrite it to mysite.com/?blog_type=a-blog-type. So mysite.com/blogs/a-blog-type/ would be displayed to the user, but mysite.com/?blog_type=a-blog-type will be loaded.
RewriteRule ^blogs/([^/]*)((/)?)$ ?blog_type=$1 [nc,l]
EDIT: So here's how your config will now look:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^blogs/([^/]*)((/)?)$ ?blog_type=$1 [nc,l]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

htaccess rewriterule

I have a WordPress theme with the following .htaccess file:
# 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
Which removes the "index.php" from the url. But I would like to remove some stuff from my start url, which looks something like "page.com/dir/dir2/page.html", to "page.com". So I tried adding
RewriteRule . /dir/dir2/page.html [L]
after the first rule, but it doesn't seem to work. What do I need to do to fix it?
RewriteRule ^dir/dir2/page\.html$ - [L]
This should work to.
Edit: little big mistake I did there. NOW it should work

Unable to exclude directory from rewriting rules

I tried to do this in many ways, but I really cannot exclude directory "progress" from wordpress rewriting rules in .htaccess. I found many solutions but none of them seems to be working.
.htaccess in root directory of wp contains:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I tried to use
RewriteCond %{REQUEST_URI} !^/(progress/.*)$
and
RewriteRule ^progress($|/) - [L]
and
RewriteCond %{REQUEST_URI} !^/?(admin|progress)/
and I even tried to put Alias /progress /home/website/progress to httpd.conf but it still not working properly. Surely, mod_rewrite is installed and working, it redirects me to index.php that shows 404 error when I trying to access the directory...
From here.
#Change
RewriteRule . /index.php [L]
#to:
RewriteRule ./ /index.php [L]
Both of your attempts should work when used correctly. As the rules are tested sequentially, you need to put the exceptional case in front of your existing Wordpress rule:
RewriteRule ^progress($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Or when using the additional condition (well ok, here the order of the conditions does not matter as they all have to be fulfilled):
RewriteCond %{REQUEST_URI} !^/(admin|progress)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Does the directory progress actually exist? What's inside it?
Where are you placing the code you have provided within the context of the .htaccess?
The line RewriteCond %{REQUEST_FILENAME} !-d will mean the rewrite rule is ignored if the request translates to a real directory.
Instead of following on .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Use the folder name that you want to exclude from wordpress htaccess rule as RewriteBase, so basically try writing following in .htaccess file for the folder "FOLDER_NAME"
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /FOLDER_NAME
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And place this .htaccess file into the folder with name "FOLDER_NAME". Please let me know if that works.

Resources