htaccess rewriting user pretty url to perform query - wordpress

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>

Related

Redirect URL without trailing post id using .htaccess

I have a scenario where I need to redirect all URLs or wordpress without trailing post id using .htaccess
Example:
https://example.com/chapter/subject-origin-chapter/1223 to https://example.com/chapter/subject-origin-chapter
and
https://example.com/subject/subject-physics/38957 to https://example.com/subject/subject-physics
Other solutions are also welcome.
Current .htaccess is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I found a solution as:
RewriteRule ^chapter/([a-zA-Z0-9-]+)/(\d+)$ /chapter/$1 [L, R=301]
RewriteRule ^subject/([a-zA-Z0-9-]+)/(\d+)$ /subject/$1 [L, R=301]

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.

wordpress redirect loop on mod rewrite

I have wordpress setup to change the admin url to www.mydomain.com/admin. But now I have updated the permalinks to use the post name instaed of the id I get a redirect loop when I try to access the admin page. The problem is with the extra rules wordpress asked me to add to the .htaccess but I am unsure how to change it to allow both rules.
Wordpress 3.5.2 rewrite rules as below.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteRule ^admin/(.*) wp-admin/$1?%{QUERY_STRING} [L]
Anyone know what i need to do to allow both rules?
Was trying out a few solutions and it would seem that having the rewrite rule for changing the admin url to www.mydomain.com/admin BEFORE the mod-rewrite rules is the key to allowing both rules to work together. See updated/fixed .htaccess code below.
RewriteRule ^admin/(.*) wp-admin/$1?%{QUERY_STRING} [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Redirect of URL with parameters to URL without -- plus Wordpress

I have rebuilt a website that used some custom php to serve up individual store pages with urls like this:
http://thedomain.com/storedetails.php?storeNum=1
The new site is powered by Wordpress, and each individual store will live in a named subdirectory, like this:
http://thedomain.com/stores/gothamcity
I have been trying solutions I have found on stackoverflow that all seem to be variations on this: 301 Redirect of old url with parameters to a path without parametes
Here's an example:
RewriteCond %{QUERY_STRING} ^storeNum=18$ [NC]
RewriteRule ^/storedetails\.php$ http://www.thedomain.com/stores/gothamcity? [L,R=301]
But so far nothing works. I continue to get "page not found" 404 error pages in Wordpress.
My best guess is that something in Wordpress's part of the .htaccess is throwing this off? Here's what they include:
# 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
Can anyone help?
This should work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^storeNum=18$ [NC]
RewriteCond %{HTTP_HOST} .*
RewriteRule ^storedetails\.php http://www.thedomain.com/stores/gothamcity? [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I suggest you use a RewriteMap though to map store numbers to names.

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!
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!

Resources