Needed htaccess rewrite to preserve migration to wordpress - php

I have a website with this kind of querystring:
http://www.example.com/index.php?action=show&id=4577
I am moving to wordpress with this blog format:
http://www.example.com/archives/4577
But i should preserve wordpress rewrite conditions
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This solution worked for me (i realized by my own...):
RewriteCond %{QUERY_STRING} action=show&id=([0-9]+)$
RewriteRule .*$ /archives/%1? [NC,R,L]

Related

SEO friendly URL parameters via .htaccess

I want to rewrite the URLs from:
https://www.example.com/bibliorafturi/?wpsolr_fq%5B0%5D=pa_culoare_str%3AAlbastru
to this:
https://www.example.com/bibliorafturi/Albastru
Another example:
https://www.example.com/bibliorafturi/?wpsolr_fq%5B0%5D=pa_culoare_str%3AAlbastru&wpsolr_fq%5B1%5D=pa_brand_str%3AESSELTE
to this
https://www.example.com/bibliorafturi/Albastru/?wpsolr_fq%5B1%5D=pa_brand_str%3AESSELTE
So I need only pa_culoare_str to be SEO friendly.
How can I do this with .htaccess?
UPDATE:
This is my .htaccess file now:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /(.*)/(.*)/(.*)/?$
RewriteRule ^ /%1?wpsolr_fq[%3]=pa_culoare_str:%2 [QSA,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Thank you!

Redirecting all .html files to root folder using htaccess

My .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^pharmacy/?(.*)$ /wp-content/plugins/swift-mailer/lib/classes/$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
After reading this answer I tried adding RewriteRule ^(.*)\.html$ / [L,R=301] after RewriteCond %{REQUEST_FILENAME} !-d which redirects html files to root folder but other URL stops working.
PS: I am not used to with .htaccess files.
You should put new RewriteRule before RewriteConds because it does not need any condition for redirect.
Moreover, you save working old redirect rules. RewriteCond affects only 1st RewriteRule after it. Placing new RewriteRule after RewriteConds make old RewriteRule be acheived in all cases without any restriction.
So, the fragment of your htaccess should look like
RewriteRule ^(.*)\.html$ / [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Put the root redirect code above the WordPress code like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^pharmacy/?(.*)$ /wp-content/plugins/swift-mailer/lib/classes/$1 [L]
# This will check that the .html is not a true file
# and if so, redirect to root
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.html$ / [L,R=302]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Note, use R=302 (temporary redirect) until you are satisfied with your redirects. Otherwise, you might have 'permanent' redirects to inadvertent places.
If I understand clearly, your .htaccess now looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^pharmacy/?(.*)$ /wp-content/plugins/swift-mailer/lib/classes/$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ / [L,R=301]
RewriteRule . /index.php [L]
</IfModule>
As RewriteCond rules only affect one RewrtieRule below them, this rule
RewriteRule . /index.php [L]
now applies to every request. Simply change your .htaccess to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^pharmacy/?(.*)$ /wp-content/plugins/swift-mailer/lib/classes/$1 [L]
RewriteRule ^(.*)\.html$ / [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

exclude folder in wordpress using htaccess failed

I created a folder named folder1 after entering the root. At that level of directory there's my .htaccess, I googled and I paste the following line at
..
RewriteRule ^index\.php$ - [L]
// this
RewriteCond %{REQUEST_URI} !(folder1|folder2|folder3) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
..
But when I go to mydomain.com/folder1, it returned 404 of my wordpress?
You should post all of your code so that we can make sure there aren't other rules conflicting. Not just the part you can't get working.
Try using THE_REQUEST and try it this way and put it above your wordpress rules. So if it matches then do nothing.
RewriteCond %{THE_REQUEST} ^(GET|POST) /(folder1|folder2|folder3) [NC]
RewriteRule ^ - [L]
I found this, exclude subfolder with wordpress .htaccess and workds for me.
I put this before WordPress rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/yoursubdirectoryname1/(.*)$
RewriteRule ^.*$ - [L]
</IfModule>
and here is WordPress rules:
# 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
My complete .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/yoursubdirectoryname1/(.*)$
RewriteRule ^.*$ - [L]
</IfModule>
# 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
source: I found this code on WordPress forum.

Redirect htaccess rule

I wish to redirect a website link onto another.
my htaccess rule up to now is
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
RewriteRule ^/the-terminal/demo/ http://example.com/wordpress/project-overview/ [R,L]
I wish to redirect the link http://example.com/wordpress/the-terminal/demo to http://example.com/wordpress/project-overview
Kindly provide me a solution. You will be a lifesaver.
Thanks
If /the-terminal/demo is a Wordpress page, the following applies:
The previous rewrite rule is being matched, so the second rule is skipped:
RewriteRule . /wordpress/index.php [L]
The [L] flag tells Apache to stop the rewrite process immediately and ignore any further rules.
To solve this, move your rule immediately after RewriteBase
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule the-terminal/demo http://example.com/wordpress/project-overview/ [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

What changed my WordPress .htaccess?

My WordPress site went down with an error 500. After looking around, I restored the .htaccess and that fixed it. My question is what in WordPress could have changed the .htaccess by itself? Do plugins sometimes do this? What about adding sub-domains within CPanel? Any way to prevent this?
The .htaccess was originally this:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
It keeps getting changed to this:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]
Permalinks as well as any Cache plugins can do that. However, you should have saved the "messed-up" .htaccess so that we could see the contents of that, vs. what it was originally...
Here is the messed up one
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Here is the good one
# 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
My .htaccess file kept getting reset like that because my site was hacked. Here's a potential solution: https://wordpress.stackexchange.com/a/191570/20963

Categories