How to rewrite URL and add slug 'blog' in htaccess? - .htaccess

How to add in WordPress a slug (blog) only if the entry has a link date
https://example.com/2016-02-05-nowe-zycie-po-przeszczepie/
to
https://example.com/blog/2016-02-05-nowe-zycie-po-przeszczepie/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Try something like the following at the top of your .htaccess file, before the existing WordPress directives:
# Prefix "/blog" to URL if date at start of URL-path
RewriteRule ^\d{4}-\d\d-\d\d- /blog%{REQUEST_URI} [R=302,L]
Note that this is a 302 (temporary) redirect. Only change to a 301 (permanent) redirect - if that is the intention - once you have confirmed this works as intended. 301s are cached persistently by the browser so can make testing problematic.

Related

RewriteRule works in .htaccess not in another included file?

I have a server with Magento and Wordpress.
Magento is in the website root /, Wordpress under /blog/.
Various Redirect 301 and RedirectMatch permanent are specified in a file in the website root. This file is "included" from the Apache vhost configuration:
Include /var/www/html/myredirects
Some pages from Wordpress should be visible from the website root, so that
http://www.mymagento.com/pretty-url/
will show the same content as
http://www.mymagento.com/blog/pretty-url/
This is currently achieved using in the .htaccess file using some rewrites as follows:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^pretty-url/ /blog/pretty-url/ [L]
[...more like the above here and the common Magento rewrites follow...]
RewriteRule ^api/rest api.php?type=rest [QSA,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^index.php
RewriteCond %{REQUEST_URI} !\.(html|jpg|png|gif)$
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteRule ^(.*)$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
Is it possible to move just the RewriteRule lines that show some pages from the blog in the root to the myredirects file?
I tried moving them with and without the part
Options +FollowSymLinks
RewriteEngine on
and in both cases it does not work, either a blank page or a Server Error is the result.
I cannot find in the Apache documentation what is the reason why whatever is defined in the .htaccess makes it work. Maybe it's because of the processing order?
Edit, as asked how myredirects looks like (please note these redirects work already):
They are a bunch of URLs with different product categories from Magento plus some related to the /blog/, they all look like these:
# These are Magento
RedirectMatch permanent /product-category/sub-category-1/sub-category-2/(colorA|colorB|colorC) /sub-category-2/$1
Redirect 301 /someurl/otherurl/ /another/url
# These are for Wordpress
Redirect 301 /blog/tag/some-tag/ /blog/some-tag/
Redirect 301 /blog/tag/another-tag/ /blog/another-tag/
Those for the blog are only redirecting the /tag/ part and nothing else.
And yes, it's only some of the /blog/ URLs that should be shown also in the website root, others will stay under /blog/ (I know there will be duplicate content for those shown in both paths).

Force WWW when URL contains path using .htaccess

I'm having a problem with my URL and my sessions.
I wish to have ALL website pages be forced to use www. As it looks like now, the website looks like this:
www.example.com into www.example.com
example.com into www.example.com
www.example.com/example/ into www.example.com/example/
example.com/example into example.com/example (this is what's wrong)
This is what my .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]
RewriteCond %{HTTP_HOST} ^wewent\.net
RewriteRule ^(.*)$ http://www.wewent.net/$1 [L,R=301,NC]
</IfModule>
# END WordPress
Because the URL does not redirect properly I get double up with sessions one for www and one for the website without. How can I prevent this the best way?
It seems to look ok but one thing you should do is always put your other rules before the wordpress rules as a habit. When using wordpress it should generally be the last set of rules since it does all the routing. Now for the redirect, you should probably use 302temporary which will remove any current cache and verify that your redirects are working properly. Then you can change it to 301 for permanent once it's working correctly.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.wewent\.net [NC]
RewriteRule ^(.*)$ http://www.wewent.net/$1 [L,R=302,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Htaccess redirect after Magento site moved to subdirectory

I've moved my site with all its contents from the root to a subfolder, like
www.example.com
To
www.example.com/shop
Now, I want to redirect all old pages to the new url.
What I tried in the .htaccess of the root, but did'nt work:
RewriteEngine On
RewriteRule ^(shop)($|/) - [L] // To prevent loops
RewriteRule ^(.*)$ http://www.example.com/shop$1 [R=301,L]
But now, all old pages redirect to example.com/shop
Edit:
After it was moved, I had to add following code to the htaccess of subdirectory to make it work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /shop/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shop/index.php [L]
</IfModule>
You can use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} !/shop/ [NC]
RewriteRule !^shop/ /shop%{REQUEST_URI} [NC,R=301,L]

.htaccess redirect - how to go from /about to /our-story

I'm trying to make it so that anything at /about goes to /our-story.
I think my .htaccess redirect rule is correct for 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]
The line is here:
RewriteRule ^about.*$ http://domain.com/our-story/ [R=301,L]
</IfModule>
However it doesn't seem to work. My suspicion is that this is due to Wordpress being active. I tried to put it inside the Wordpress block though, and still it doesn't work.
What do I have to do to redirect from /about to /our-story?
The rule is correct, but they need to go before the wordpress rules because you are redirecting while the wordpress rules routes everything into /index.php. Any request URI that starts with /about will end up getting routed to wordpress and thus never get redirected. Just switch the order:
RewriteRule ^about.*$ http://domain.com/our-story/ [R=301,L]
# 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>

htaccess redirection subfolder

Hi I am looking to the following in .htaccess:
RewriteRule ^pqr$ /pqr.php
[without 301 redirect, because I want the user to see "pqr" in the broswer and not "pqr.php"]
The problem is that when I try the above .htaccess code, /pqr automatically redirects to the "pqr" subdirectory (/pqr/). I'm looking for a way to stop that automatic redirection.
Do you know how to do that with htaccess?
my current htaccess code
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Wordpress_Work/placewise/wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /Wordpress_Work/placewise/wordpress/index.php [L]
</IfModule>
# END WordPress
That should do it:
RewriteEngine on
RewriteRule ^/([^/\.]+)/?$ $1.php [L]

Resources