Redirect htaccess rule - php

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>

Related

why is .htaccess not working on live site?

I'm needing to rewrite urls to be "pretty" instead of queries: they need to be /kw/blah instead of kw.php?kw=blah. I've tested the code several times with online checkers, and they say it works/is syntactically correct.
The two sites I used to check are: https://htaccess.madewithlove.be (url testing was with /kw/melbourne-web-design and http://www.htaccesscheck.com
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} blueshiftwebservices\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://blueshiftwebservices.com/$1 [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^kw/([^/]*)$ landing.php?kw=$1 [L]
</IfModule>
# END WordPress
I'm getting an error of 404 Page not found only with the pretty url, not the url that contains the query.
The live site definitely refers to the .htaccess file, so I'm lost at what to do?
The rule RewriteRule . /index.php [L] matches all URLs. So you must insert the rule RewriteRule ^kw/([^/]*)$ landing.php?kw=$1 [L] before it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# redirect to https
RewriteCond %{HTTP_HOST} blueshiftwebservices\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://blueshiftwebservices.com/$1 [R,L]
# are you sure you need it?
RewriteRule ^index\.php$ - [L]
# make it "pretty"
RewriteRule ^kw/([^/]*)$ landing.php?kw=$1 [L]
# process the rest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Needed htaccess rewrite to preserve migration to wordpress

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]

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>

htaccess rewrite url rule

I'm new to htaccess and I need some help with a rewrite rule.
I wish to rewrite:
/all-accomodation/anything/ to: /portfolio-gallery/anything/
The 'anything' could be any slug.
One thing to note is that I have a page with a slug of 'all-accomodation' and when requested by itself shouldn't rewrite.
Here is my current 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
Thanks in advance,
Dave
You don't want to edit Wordpress's rewrite. Above that block, you can try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^all-accomodation/(.+)(/?) /portfolio-gallery/$1$2 [L]
</IfModule>
This will 301 redirect all requests.

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