Not sure if this is the right section of Stackoverflow to ask my question...
But here it is:
So I am using the below on the .htaccess file:
RewriteEngine On
RewriteRule ^sale?$ /discount-page/
So that when people visit example.com/sale page, they see content from example.com/discount-page/
But when I visit example.com/sale it shows 404 error saying that the URL /discount-page is not available on this server...
Why is it happening?
Here's how my entire .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
Can anyone help please?
When using WordPress, you can't simply rewrite the URL in .htaccess to the %postname% (the real URL) since WP still looks at the REQUEST_URI in order to route the URL. Even though you are rewriting /sale to /discount-page/ (the actual URL), WordPress sees /sale (the requested URL) - which doesn't exist inside WP; hence the 404.
Although not your intention, you could change this to an external redirect to get around this problem (which also avoids a potential duplicate content issue). For example:
RewriteRule ^sale$ /discount-page/ [R,L]
(I removed the ? in ^sale?$, as that does look erroneous. Or do you really want to match /sale or /sal?)
Alternatively, you could try rewriting to the underlying "plain" permalink. ie. explicitly pass the %post_id%. This is different to rewriting to the %postname%, since WP shouldn't need to check the REQUEST_URI in order to route the URL. For example:
RewriteRule ^sale$ /index.php?p=123 [L]
Where 123 is the %post_id% of your discount-page. By rewriting directly to index.php, you are effectively bypassing WP's front-controller.
Note that this must go before the standard WordPress directives in .htaccess (aka the front-controller).
However, I still feel there should be a more WordPress-like way of doing this, which is why I initially suggested asking this question over on the WordPress Stack. However, if you do that, don't mention ".htaccess". What you are really creating is a URL alias or something like that. For example: Have two different URLs show the homepage
Related
Long version (you can skip to TL;DR if you want to):
I am working with a Wordpress site that was set up by someone else. The website has multiple pages where page has tabbed content which is accessible through #. For eg:
www.example.com/services/category1/#tab-service1
www.example.com/services/category1/#tab-service2
www.example.com/services/category2/#tab-service1
www.example.com/services/category2/#tab-service2
www.example.com/services/category2/#tab-service3
Now, when search engines index they are indexing only www.example.com/services/category1/ and www.example.com/services/category2/. This creates a problem where we cannot have search engines point directly to the content within a given tab. What we want is for search engines to show links that takes users directly to (say) www.example.com/services/category2/#tab-service3.
Now, I don't think google can index such # based content on its own. So, I am thinking of using apache rewrites to try to resolve this issue. I have access to .htaccess file only (from a config perspective).
TL;DR
How to redirect www.example.com/services/category1/service3/ to www.example.com/services/category1/#tab-service3 using apache redirects (I have access to .htaccess file)?
This is what I am trying but it's not working:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/services/category1/([a-z0-9])/? [NC]
RewriteRule .* /services/category1#tab-%1 [R,NE,L]
Someone also adviced to look into pushState server config to fix this. I am not sure how to use pushState.
UPDATE:
I have updated the rewrites to the following but it still doesn't work. It keeps showing Wordpress' 404 page
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /domainfolder/
RewriteCond %{REQUEST_URI} ^/services/category1/([a-z0-9]+)/?$ [NC]
RewriteRule ^/services/category1/([a-z0-9]+)/?$ /services/category1/#$1 [NE,R,L]
</IfModule>
Your %{REQUEST_URI} regex is wrong. The pattern ^/services/
category1/([a-z0- 9 ])/? matches /services/category1/{any 1 char of a-z or 0-9} format followed by an optional slash. So this does not match your request /services/category1/service3 but matches /services/category1/a/ .
You should be using
^/services/category1/([a-z0-9]+)/?$
I was wondering if someone could help us out with this one.
We have a site that has lots of insecure links in various pages across the site. Some pages are also including images from http sources and not https.
An example page is here: https://mindfulpresenter.com/mindful-blog/the-10-most-important-things-in-presenting-today/ - on here you will see that there are some links to http pages and the page is sourcing an image from http.
I was hoping that all of these issues can be resolved by making changes to the htaccess file.
The current htaccess file looks like this:
# 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 the issues be resolved by making changes to the htaccess file? If so, what do I need to add to it?
Many thanks
James
It appears, that the links are in the content of your pages / posts which is stored in the database as text. I recommend doing a search & replace to achieve a "clean" state.
There are some plugins that parse the content and create those links dynamically but i don't think that is the most elegant solution.
There are a lot of plugins that may help you. I use SSL Insecure Content Fixer on my website and it works great.
You can, in fact, modify the contents of the page as it is being served, using mod_substitute: https://httpd.apache.org/docs/current/mod/mod_substitute.html
In particular, you'd do something like:
Substitute s/src="http:/src="https:/i
See the mod_substitute docs for further information and examples.
I found a 'Search and Replace' plugin for WordPress which searched the database and replaced all of the HTTP entries.
I have a website done in Wordpress and I need to make some changes in the fiendly URLs.
I’ve created a page from the admin panel named detail, this page reads the template file detail.php from the templates folder.
The URL that is currently mounted is http://www.domain.com/detail/1234/ and I need that it could be accessed as http://www.domain.com/anything/1234/.
The following lines have been generated by Wordpress but I don’t understand them and I don’t know how to modify them for my purpose:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
First you should really understand what those rules are doing and what you really want to achieve. Then you can try to change the system to fit your needs.
IfModule ensures everything inside is processed only when mod_rewrite Apache module is present. All the other directives are from this module.
RewriteEngine On enables URL rewriting.
RewriteBase / tells the engine that the rules operate inside root. See also the general question on how RewriteBase works.
RewriteRule ^index\.php$ - [L] means that no more rules should be processed ([L]) if the current URL to be rewritten is index.php. No rewrite takes place. RewriteRule directive accepts a regex. See also regex tag here on SO.
All RewriteCond directives apply to the following RewriteRule. Unless [OR] flag is added, they must be all satisfied at the same time to execute the rule. In this case they mean:
Requested resource is not a regular file.
Requested resource is not a directory.
Rewrite any (at least one character long) URL to index.php. This is the last ([L]) rule to be processed.
When adding new RewriteRules, you probably want to use the WordPress way of doing this, as described in Zac’s answer. Figuring out the right rule by analogy to the many examples in the manual or here on SO should not be hard.
Put into functions.php maybe a better idea:
functions.php
function setRewriteRule($orgRules){
return array( '/([^/]+)/([0-9]+)/?' => 'index.php?post=$matches[1]' ) + $orgRules;
}
add_filter('rewrite_rules_array', 'setRewriteRule');
Then you just need flush the rewrite rules, I usually use 'rewrite-rules-inspector' plugin.
This should solve your problem give it a try ... you can write your own custom permalink without adding any code also if someone tries to access the page via old URL they will be redirected to the new one.
WordPress Custom Permalinks
After installing this you just have to go into pages and type your own URL below the heading
I find myself migrating a static site to a WordPress-based website fairly frequently these days. Once I've finished development of the WordPress website on a test server, I then want to install it on the current website domain, but without affecting the old static site- until the site goes live.
My idea is to move the current non-WordPress website to a folder /old/ and get all requests to rewrite to this folder (and so for SEO purposes keeping all the urls the same).
To allow myself and other authorized people through I want to check for a cookie (that will be set via a simple "login" php file since I can't rely on static IP addresses) or if the current date and time is after the live date then let the WordPress rewrite rules take their course to give access to the WordPress website.
This will also allow me to add a simple countdown timer on the current or old site after which the new site will go live automatically at live date.
My simple login.php script is like this:
<?php
setcookie("login", "loggedin", time()+3600,"/");
?>
<h3>Welcome, you are logged in</h3>
<p>Click here to visit the home page</p>
I thought the following might be the best way to acheive this, but it doesn't work (I get a Internal Server Error). The first section checks to see if the login cookie exists and if it doesn't and the current date and time is before 8pm on July 20 2012 it will rewrite all requests to the /old/ folder. I've modified the WordPress rewrite section to exclude the /old/ folder.
RewriteCond %{HTTP_COOKIE} !^.*login.*$ [NC]
RewriteCond %{TIME} < 20120720200000
RewriteCond %{REQUEST_URI} !^/old/
RewriteRule (.*) http://domain.com/old/$1 [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/(old/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Can someone help me with what I want to achieve?
The RewriteCond directive takes 2 arguments, but you are giving it 3 when comparing %{TIME} and that's causing apache to return a 500 server error.. If you look at the lexicographical comparisons in RewriteCond, there can't be a space after the comparitor (mod_rewrite is kind of dumb like that). So that line needs to look like this:
# no space here -----V
RewriteCond %{TIME} <20120720200000
That should deal with the 500 server error.
The other thing about your rules is that the rule's target, http://domain.com/old/$1, will cause apache to 302 redirect because you are including the http://domain.com in the target. If you want it to rewrite internally (so the URL in the browser's address bar doesn't change), you need to remove that bit.
A simpler way would be to install plugin called Maintenance mode
Then create a 503.php for the theme.
add this to the beginning of the file:
<?php if (!is_user_logged_in())
{
header("Location: http://example.com/old");
}
else
{
//WHATEVER YOU LIKE
}?>
That way, logged in users can browse the WP-site, and others will get redirected to the /old website.
users must login beforehand # http://example.com/wp-login.php
I'm having a Wordpress problem regarding permalinks.
When I have a post whose permalink is say, /2009/10/podcasts, trying to access /podcasts redirects to /2009/10/podcasts. Is there any way to stop this behavior so I can handle it as a 404?
I'm using a custom 404 handler that checks if the request is a 404 error and executes a Kohana request from within Wordpress.
I just got the answer on the WP forums. It's
remove_filter('template_redirect', 'redirect_canonical');
just checked with a default installation of wordpress - and indeed it seems to be a default behaviour of wordpress to look for the path anyways - no matter what directories you add hide. So also replacing the "/2010/01/some-long-url" by "/error/some-long-url" will redirect the reuest to "/2010/01/some-long-url".
Anyways - I can still suggest two workarounds:
1) if you really want to get a real 404 error you can use the redirect method in your htaccess to forward the request to a non-existing url - just add the one redirect-line like this:
Redirect /podcasts /podcasts-error
all together the htacces could then look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Redirect /podcasts /podcasts-error
</IfModule>
2) If you don't really need the 404 error you may also just add a page and give it the exact same url - in this case it would be "podcasts". That would de-activate the forwarding. You could then add your own custom 'error message' to that page..
Greetz, t..