After searching StackOverflow and trying multiple Regex approaches, I can't seem to get a redirect that will make every example.com/example.html page on a site instead point to simply example.com/example. My .htaccess file looks like this:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
My first attempt was
RedirectMatch 301 (.*)\.html$ https://example.com$1
But while it worked for the individual .html pages, it resulted in the home page redirecting to https://example.com/index and that is a 404.
My latest attempt was to add this at the bottom
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
That didn't work at all. Then I put that same line BEFORE the line
RewriteRule . /index.php [L]
So the block looks like this
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule . /index.php [L]
But that just resulted in the site loading really slowly and when it loaded the images and stylesheets were broken.
Is there a way to do this that I'm just not understanding?
You may use this redirect rule before your existing rule:
RewriteEngine On
# removes .htm, .html, index.htm or index.html - case ignore
RewriteCond %{REQUEST_URI} ^/(.*/)?(?:index|([^/]+))\.html?$ [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Related
I'm setting up an apache virtualhost, and I want it to redirect all requests to index.php passing as a parameter the original URL if matches a specific pattern.
Considering the pattern is (quote-less) "PaTTern[0-9]", this is what I have tried:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(PaTTern[0-9]).*$ /index.php?$1 [NC,L]
RewriteRule ^(.*)$ /index.php [NC,L]
What I am getting is an Error 500. Any ideas?
The last line is most probably causing rewrite loop (since it is unconditional).
Since you don't want any rewrites in case the request already goes to /index.php, add another condition:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(PaTTern[0-9]).*$ /index.php?$1 [NC,L]
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$ /index.php [NC,L]
I have migrated my site to a wordpress site and set up the permalinks to be the post name. I need to add a rewriteRule to take anything from a cgi directory and just send them to the home page Ive tried:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
RewriteRule ^cgi(.*)$ http://example.com
Removing the [L] from the wordpress rule and adding the last line but it still doesnt work. Can someone tell me what I'm doing wrong?
Keep L flag as before and your cgi rule should be placed below RewriteBase line:
RewriteEngine On
RewriteBase /
RewriteRule ^cgi(.*)$ / [L,NC,R]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I'm trying to create a rewrite rule that redirects vistors from one url to another on an Ecwid store, but I can't seem to get the syntax correct.
I'd like to redirect visitors from :
http://domain.ca/category/#######
to
http://domain.ca/products/#!/~/category/id=#######&offset=0&sort=normal
I've tried several variations below, but all generate a 500 Internal Server Error
Rewrite Rule ^/category/(.*)$ http://domain.ca/products/#!/~/category/id=$1&offset=0&sort=normal [R=301,L]
Rewrite Rule ^/category/(.*)$ http://domain.ca/products/#\!/~/category/id\=$1&offset\=0&sort\=normal [R=301,L]
Rewrite Rule ^/category/(.*)$ /products/#\!/~/category/id\=$1&offset\=0&sort\=normal [R=301,L]
Rewrite Rule ^/category/(.+)$ http://domain.ca/products/#!/~/category/id=$1&offset=0&sort=normal [R=301,L]
Here are the existing rewrite's in the htaccess file, using anubhava's example below
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^category/(.+)$ /products/#!/~/category/id=$1&offset=0&sort=normal [R=301,L,NE]
</IfModule>
You have syntax issues. Use this rule:
RewriteEngine On
RewriteBase /
RewriteRule ^category/(.+)$ /products/#!/~/category/id=$1&offset=0&sort=normal [R=301,L,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I have an .htaccess file in the root of the website that looks like this
RewriteRule ^some-blog-post-title/ http://website/read/flowers/a-new-title-for-this-post/ [R=301,L]
RewriteRule ^some-blog-post-title2/ http://website/read/flowers/a-new-title-for-this-post2/ [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
## Redirects for all pages except for files in wp-content to website/read
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/wp-content
RewriteRule ^(.*)$ http://website/read/$1 [L,QSA]
#RewriteRule ^http://website/read [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
My intent is to redirect people to the new blog post location if
they propose one of those special blog posts.
If that's not the case
then they should be redirected to http://website.com/read.
Nothing from http://website.com/wp-content/* should be redirected.
So far conditions 1 and 3 are being met. How can I meet condition 2?
Note: I want to redirect to /read even for content that exists to prevent any images, CSS, or javascript from working, except those in /wp-content
Make your first rule as:
## Redirects for all pages except for files in wp-content to website/read
RewriteRule !^wp-content/ http://website.com/read%{REQUEST_URI} [NC,L,R=302]
Every since an upgrade to WordPress 3.3 URLs are not redirecting as they should.
Changed: domain.com/2010/10/postname/ to: domain.com/postname/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
The problem was due to the leading slash and not using $3
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.+)$ /$3 [NC,R=301,L]
There's a script here you can use to generate .htaccess rules if you want to change permalinks to the /%postname%/ structure.
http://yoast.com/change-wordpress-permalink-structure/
My permalinks were exactly the same as yours, I used this tool to change them and it is working well.
The last rule will never get applied if the previous rule matches. Assuming that the http://domain.com/2010/10/postname/ request doesn't match a file or directory, the RewriteRule . /index.php [L] is going to rewrite the URI to /index.php thus it'll never get to your rule. Try moving your rule up to the top, just below RewriteBase /, and duplicate the !-f/!-d conditions, so that it looks like this:
RewriteBase /
# for 301 redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
# the rest of the rules
RewriteRule ^atom.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss2.xml$ feed/ [NC,R=301,L]
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/handle [R=302,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Also, if this is in an .htaccess file, you need to remove the leading slash in the rule match so that it looks like this: ^[0-9]{4}/[0-9]{2}/(.+)$