So, I'm trying to add my own RewriteRule in addition to what Wordpress already has. A little backgroun. The site I am working on is built off of a custom CMS, but also has a blog that is powered by WordPress. So, heres what I have thus far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Custom rewrite for locatons
RewriteRule ^locations/(.*)$ /locations2.php?info=$1
# Wordpress rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
I also got rid of a couple lines of the Wordpress Rewrite code that seemed unecessary (everything still works....I think). But yeah, it seems like it is overwriting the rule that I wrote. Help?
Oh, also, I just tried to get rid of the . that is before /blog/index.php [L] and replaced it with just a ^, it worked...kind of. It loaded the blog when I went to /blog/, but when I went to something like /blog/author/admin, I got a 404.
I think you need to add the last flag [L] after your new rule, otherwise processing will continue and your request will be rewritten to '/blog/index.php'.
RewriteRule ^locations/(.*)$ /locations2.php?info=$1 [L]
Have you tried to remove the RewriteBase / and the slash before locations2.php ?
Something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
# Custom rewrite for locatons
RewriteRule ^locations/(.*)$ locations2.php?info=$1
# Wordpress rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
If the blog gets unreacheable, move RewriteBase / to after the custom rewrite. Also, I believe that if the blog is in a sub-directory, the RewriteBase should be RewriteBase /blog/
Related
I need to serve these pages:
/category/*
As
/shop/category/*
I need the url to have /shop in front of /category/ and it needs to handle all nested path structures within category also.
How can I do this via .htaccess preferred?
There is also a /shop/ page that I do not want disturbed by any .htaccess edits.
So, I just need to serve /category/ pages with the url having /shop/category/ instead, is this possible via .htaccess?
So I'm a beginner here in .htaccess rewrites, but have tried this:
RewriteCond %{HTTP_HOST} ^category/$
RewriteRule ^shop/category/(.*)$ category/$1 [L]
It doesn't do anything tho...
Also tried this:
RewriteCond %{HTTP_HOST} ^category/(.*)
RewriteRule ^$ /shop/category/$1? [R,L]
Again, no effect on anything.
I am using Wordpress, so it seems it has some power over my rewrite rule perhaps?? Anyways, here is my entire .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
RewriteRule ^shop/category/(.*)$ category/$1 [R,L]
Have tried the following within the init wordpress action also:
add_rewrite_rule('^/shop/category(?:/.*)?', '/category/$matches[1]', 'top');
No luck on this.
Ok, seems that I might be getting somewhere here...
The following add_rewrite_rule below matches only the first category, but doesn't match for child categories:
add_rewrite_rule('shop/category/?([^/]*)', 'index.php?product_category=$matches[1]', 'top');
The custom taxonomy that I am using is product_category, not wordpress default category since I don't want my product categories mixed with post categories. So, I just need to figure out now, how to match all paths (which include child categories), for example:
/category/gelest-inc/silanes-silicones/
silanes-silicones is a child of gelest-inc, how to capture this in the rewrite rule above also?
If I understand your question correctly following .htaccess should serve your requirements:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^category(?:/.*)?$ /shop/$0 [R=301,NC,NE,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Your rule says if HTTP_HOST is catagory/ then rewritehomepage (^$ matches an empty uri eg : /) to /shop/catagory. Your RewriteCond never meets as you are matching against HTTP_HOST header using URI in pattern.
If you want to redirect /catagory/ to /shop/catagory , you can use
RewriteEngine on
RewriteRule ^catagory/([^/]*)/?$ /shop/catagory/$1 [L,R]
You have the WordPress rules, and after the rules comes the "shop" rule.
When the shop URI is not a real file or directory, requests for it will already be handled by the WordPress rules and rewritten to index.php. Apache then iterates a second time with index.php, but it doesn't match the shop rule and never redirects or rewrites.
To make this work the shop rule must come first
RewriteRule ^shop/category/(.*)$ category/$1 [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Now it depends on what you want to achieve, redirect from
RewriteRule ^shop/category/(.*)$ category/$1 [R,L]
or to shop/category
RewriteRule ^category/(.*)$ shop/category/$1 [R,L]
I'm trying to remove a directory from any URLS that contain it. It's a wordpress blog.
Currently the following urls 404, and probably many others:
site.com/blog/archives/yyyy/mm/dd/
site.com/blog/archives/yyyy/mm/dd/post-title/
site.com/blog/archives/author/username
However, all of these urls work fine when they are in this format:
site.com/blog/yyyy/mm/dd/
site.com/blog/yyyy/mm/dd/post-title/
site.com/blog/author/username
So pretty much I need to take any url that has the director /archive/ in it, regardless of what comes after it, and remove it. I've tried lots of example online with no luck. I feel like I must be doing something wrong and not realizing it.
The last thing I tried was this, thinking it would be a very broad rule and work. No dice.
RewriteRule (.*)/archives/(.*) $1/$2 [NC]
Here is my current .htaccess file.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ([0-9]+)\.html /blog/?p=$1 [NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
Right before the # BEGIN WordPress line add:
RewriteRule ^archives/(.*)$ $1 [L,R=301]
I want to strip out the ?q= from my old drupal links for my site now in Wordpress
I need help with two things.
First, how do I specify a range of words instead of numbers, as in the snippet below? Once I do that will this code work to redirect ".../?q=postname" to simply ".../postname"?
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([0-9]+)$
RewriteRule .* http://www.mysite.com/?q=%1 [R=301,L]
Secondly, once i have that code working, how do I integrate it with the default Wordpress mod_rewrite (which is below)?
# 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
Try this:
RewriteCond %{QUERY_STRING} ^q=(\w+)$
RewriteRule .* http://www.mysite.com/%1? [R=301,L]
BTW I would use [R=302,L] at first until I make sure everything works as expected. If you use 301 with wrong RewriteRule browser will cache the redirect and any further RewriteRule changes would require you to restart browser for each code change to see any difference.
I have uploaded a file 'events.php' to my wordpress site (the wordpress is installed in my root directory).
I would like to make a mod_rewrite to this file so that one may go to mydomain.com/events
I tried simply doing something like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule %events$ events.php [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Which didn't work.
I also found I could add rewrite tags via wordpress using its inbuilt functions like so:
add_rewrite_rule('^events?','/events.php','top');
This also didn't work - I simply end up on my default 404 page as though its reached a non-existent page within my wordpress.
What am I missing here?
Correct me if I’m wrong, but if all you want is for someone who goes to example.com/events to be directed to example.com/events.php, than you can just do a redirect like so.
Redirect 301 /events http://www.example.com/events.php
Try the following:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^events /events.php [L]
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The directive that you had set for the URL would not match:
RewriteRule %events$ events.php [L]
Also, the directive is now below the condition that checks for an existing file, so that it never matches a direct request for events.php.
I'm trying to combine the following code so that the WordPress permalinks work in the main directory, waringis.com (top code) and a second domain, burrowpress.com, is redirected to the subdirectory 'waringis.com/burrowpress' (bottom code) -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} burrowpress.com$ [NC]
RewriteCond %{REQUEST_URI} !^/burrowpress/.*$
RewriteRule ^(.*)$ /burrowpress/$1
Right now permalinks are working in WordPress and the redirect works but images have to be direct links to waringis.com/burrowpress/images/ instead of burrowpress.com/images/ - Any help is much appreciated...
You need to swap your code blocks around. The [L] flag on the WordPress rules are stopping execution of the file at that line in their code since your special path would "pass" the WordPress REWRITE_COND statements:
RewriteEngine On
RewriteCond %{HTTP_HOST} burrowpress.com$ [NC]
RewriteCond %{REQUEST_URI} !^/burrowpress/.*$
RewriteRule ^(.*)$ /burrowpress/$1 [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You don't need two RewriteEngine On statements, but since WordPress is able to rewrite your .htaccess file (depending on how you have it setup) you might want to leave it. If you are updating your file manually, you can remove the second RewriteEngine on directive.
The important part is that I moved your special rules ahead of wordpress.
I'm not a .htaccess expert but i would say that you don't need to include RewriteEngine On twice that you can put the redirect code in the if statement. So you want to redirect to the subdomain but not have your images be kept in the subdomain is this correct?