Simple thing I'm trying to do - just upgraded a site to use wordpress, there is ONE page in particular i do not want to 301 and want to retain its original url
the original url is
/freerecords.htm
here is what i added to my htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/freerecords.htm [NC]
RewriteRule . /index.php [L]
RewriteRule ^/freerecords.htm$ /index.php\?page_id=793 [L,NC]
problem is the above isn't working -i think i'm pretty close...
i don't want to use wordpress built in php url rewriting functions - i just want to do this in the htaccess
any help is greatly appreciated
instead of directly changing from .htaccess use setting of permalinks in your admin panel to do URL rewriting
And also check the permissions to .htaccess file before doing permalink changes.
Or you can also change wordpress .htaccess file from the theme file editor provided on admin side
http://davidwalsh.name/wordpress-htaccess
If you want a 301 redirect, you need this to happen before the wordpress related rules:
RewriteRule ^freerecords.htm /index.php?page_id=793 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This redirects the browser using a 301 redirect. Like all redirects, the browser's URL address bar will now say /index.php?page_id=793.
If you didn't want a 301 redirect, your old rules have a leading slash in them that is probably making it not work. Your last line has a leading slash in the regular expression that makes it so it will never match anything:
# was here---v
RewriteRule ^freerecords.htm$ /index.php\?page_id=793 [L,NC]
Related
I am trying to do a very simple single URL rewrite.
This is it:
RewriteRule ^blog /?post_type=post [NC,L]
I would simply like example.com/?post_type=pos to redirect and display example.com/blog.
I have tried a number of different versions of the rewrite but all I have achieved thus far is that I don't get a 404 on example.com/blog but keeps going back to example.com/?post_type=post.
I have placed RewriteRule right at the top of the .htaccess file which didn't help.
These are the other rewrites I have in the same .htaccess file:
#Single URL
RewriteRule ^blog ?post_type=post [NC,L]
#http www rewrite
RewriteCond %{HTTP_HOST} ^instrumentrentalbarcelona.com [NC]
RewriteRule ^(.*)$ https://www.instrumentrentalbarcelona.com/$1 [L,R=301,NC]
# 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
Thank you #DocRoot for helping me with this. The answer is detailed in the comment thread below by DocRoot and I. Just thought I would summarise this.
I wanted to show a page with all my blog posts.
This can be done if you use the WordPress post type "Post". example.com/?post_type=post
I wanted that page to show when you go to example.com/blog.
The following Rewrite works to show the desired URL: (thank you again #DocRoot)
RewriteCond %{QUERY_STRING} ^post_type=post$
RewriteRule ^$ /blog [QSD,R,L]
But /blog was giving a 404. So;
Create a page in WordPress called "blog"
Go to: Setting > Reading > Your homepage displays > Posts page: [Select page "blog"]
Save and done.
RewriteRule ^blog /?post_type=post [NC,L]
I would simply like example.com/?post_type=pos to redirect and display example.com/blog
The directive you posted does the exact opposite. It "rewrites" (not "redirects") from /blog<anything> to /?post_type=post
To redirect based on the query string you need an additional condition that checks against the QUERY_STRING server variable because the URL that is matched by the RewriteRule pattern is the URL-path only.
Try the following instead:
RewriteCond %{QUERY_STRING} ^post_type=post$
RewriteRule ^$ /blog [QSD,R,L]
This "redirects" from example.com/?post_type=post (exactly) to example.com/blog.
The QSD flag (Apache 2.4+) is required to remove the query string from the redirected request. If you are still on Apache 2.2 then you will need to append the substition with ? instead of using the QSD flag. eg. RewriteRule ^$ /blog? [R,L].
Not also, this is a temporary (302) redirect. If this should be permanent, then change the R flag to read R=301 - but only once you have confirmed this is working OK.
I've recently gained access to a domain which used to have a very large site and product base, however the old host turned it off and all the URLs are lost. I am using WordPress on the domain (previously it used an e-com shop called Kyrptronic) and have set up a simple landing page. So far there are over 20,000 not found URLs and I need them to redirect to the homepage (except for css and jpg files that I am using on the landing page).
I tried to redirect all old links straight to homepage however this means the core wordpress files become inaccessable and when you try and click anything in the backend E.g. Plugins - it redirects to the homepage. Here is the code I used in the htaccess file.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js|xml)$
RewriteRule (.*) / [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Is there a htaccess code snippet that can redirect all URLs other than jpgs, pngs and NOT the entire wordpress and website directory with .php file extensions?
The site has too many not found URLs to do one by one.
Hope this makes sense, it's my first post on here!!
Thanks
You could try something like this (it must e above the wordpress routing rules):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js|xml|php)$
RewriteRule ^ / [R=301,L]
For whatever reason or another I haven't been able to ascertain, my company has decided to go with wordpress for one of their websites. They asked me to build an affiliate application on the same domain, which I did. Everything works great with the exception of this dilemma:
wordpress is installed in the root directory. All pages, videos, sales, etc are made from within wordpress pages.
The affiliate application is in a subdirectory /aff/ and affiliates' pages are found at mydomain.com/aff/index.php?aff=affiliateusername
Affiliates (and their leads) should be able to load their pages simply by typing in www.mydomain.com/affiliateusername but I am struggling to understand how to translate wordpress htaccess rules to do this.
Obviously the best order in which to have this work is for wordpress to first determine if there are any blogs/posts/pages that match the url term FIRST, and if none is found, then to redirect all else to www.mydomain.com/aff/index.php?aff=whatever
Here's what I was finally able to come up with that works for the index page and for the affiliate pages, but does not correctly load any wordpress pages other than index.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(images|wp-admin|wp-content|wp-includes|go|compliance\.html)($|/) - [L]
#RewriteRule ^([^/].*)$ /aff/index.php?aff=$1 [R,L]
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You can tell I've attempted to exclude certain directories from the rewrite but have not been successful. I've read other advice via Googling, to put the redirect rules ahead of the wordpress block, but there are few issues. When I put this line ahead of the # BEGIN WordPress line, I get an endless redirect loop which keeps going to /aff/index.php?aff=aff/index.php?... etc (this is the same line I use for the same affiliate application on a different, wordpress-free, domain)
#RewriteRule ^([^/].*)$ http://www.mydomain.com/aff/index.php?aff=$1 [L]
I feel like I'm missing something terribly obvious. Should I just be setting up wordpress to redirect all 404's to /aff/index.php?aff=originalrequest? How would I go about doing that?
Thanks in advance.
You are using RewriteRules incorrectly in place of RewriteConds. Adding them in between the WordPress rules is surely breaking your blog as well. Change your .htaccess code to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)$ /aff/index.php?aff=$1 [R=301,QSA,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
If you want the URL to stay mydomain.com/affiliate remove the [R] and use only [QSA,L]. I've updated the rules above to show how to exclude a path from affiliate redirection. The following
RewriteCond %{REQUEST_URI} !^/blog\b
excludes all URLs pointing to /blog or its sub-directories /blog/sub/dirs from redirection. If there are root-level .php pages present (even if they are few) the exclusion can more easily be handled by changing the rule to
RewriteRule ^([^/.]+)$ /aff/index.php?aff=$1 [R=301,QSA,L]
assuming that a . and a / can never be present in an affiliate name.
I am in the process of moving my current static site to Wordpress, using the same domain name.
Both old and new sites have their content in the root folder and also have identical file names, but on the new (Wordpress) site, I have removed the .php extension that exists on the current site and added a trailing slash, as per Wordpress permalinks.
So I need to redirect all the old pages (for example):
/contact-us.php -> /contact-us/
I have tried the well-documented options such as (and variations on this):
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /$1 [R=301,L]
But this seems to be conflicting with the Wordpress .htaccess rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
When I try to access www.site.com/contact-us.php, I am getting either an infinite loop OR a redirect to www.site.com/index (no .php, no trailing slash), which Wordpress displays as a 404 page.
I have tried with and without the first set of Rewrite conditions (as I see they are duplicated in the Wordpress rules), and also placed my rule before and after the Wordpress rules. No joy. I've also been Googling for the last few hours but no one seems to have addressed this specific problem. I do usually find what I am looking for by searching, so it's in desperation that I'm actually posting (and which is why it's my first post!)
If anyone can help out, I would be very grateful.
It appears you will run into loops when trying an .htaccess redirect because of the php suffix. Maybe someone else knows a solution.
But try a plugin called Redirection « WordPress Plugins. Out of curiosity, I tried a redirect from contact-us.php to contact-us/ and it worked fine. The plugin redirects via php rather than writing to .htaccess, and that may be the difference.
I use the plugin all the time to mostly log 404's and do a few redirects. It doesn't take too much site overhead.
Be sure and set Options to not do "URL Monitoring," as that will set up automatic redirects to to posts/pages that have their permalinks manually changed. And set your 404 logging to a day or two, because the logs can quickly get big and result in huge database tables.
Try using this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Rewrite /something.php to /something/
RewriteRule (.*)/(.*)\.php$ $1/$2/ [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This method would rewrite:
/contact-us/test/hello.php -> /contact-us/test/hello/
If you don't want subdirectory rewriting replace line 6 with:
RewriteRule /(.*)\.php$ /$1/ [R=301,L]
I recently switched over my website to Wordpress. My old files were www.example.com/about.php and now its www.example.com/about/. I need to redirect incoming links from the .php extension to just the / for ALL my pages preferably using .htaccess.
What I have:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Whats in my .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
I guess I don't know where I'd put it in my .htaccess file either.
Thanks!
The code you have does not redirect anything. It takes a request that might be for a php file and internally appends the .php extension. Nothing happens on the browser because you've not told it to do anything. This is a 2 step process here. See the top part of this answer for a short explanation.
In order to redirect, you need to match against the incoming request, not the URI (which could have been rewritten be previous rules or iterations):
RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /(.*)\.php($|\ )
RewriteCond %{REQUEST_URI} !wp-admin
RewriteRule ^ /%2/ [L,R=301]
So when someone types http://www.example.com/about.php in their browser's URL address bar, the request will look like:
GET /about.php HTTP/1.1
and the %2 backreferences about and redirects the browser to http://www.example.com/about/ (note the trailing slash) and the address bar changes.
What happens then is the browser makes ANOTHER request but this time for http://www.example.com/about/ and the server gets the URI /about/. Now you need your rule to internally rewrite it back to the php file. Unfortunately, your rule doesn't handle the trailing slash, so you need something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
These would all go before your wordpress rules. The wordpress rules route everything to index.php and that would wreck any URI you are trying to rewrite.
You are doing things the wrong way around. You are actually rewriting urls that end end without php to files that do end in .php.
You'd need to do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*).php$ $1/ [NC]
I also removed the check if the file you are redirecting to exists as well as the [L] flag, because it's likely that wordpress is also doing its own rewriting, which means that you don't want this to be the last rule processed and that you won't be able to find the file on the filesystem.