301 redirect doesnot work in htaccess - apache

I want to redirect from https://*****.com/lp/index.html
to https://*****.com/lp/
so I put these two line in .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /lp/index\.html\ HTTP/
RewriteRule ^/lp/index\.html$ /lp/ [R=301,L]
and now the whole redirect block in my htaccess file is like this:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /lp/index\.html\ HTTP/
RewriteRule ^/lp/index\.html$ /lp/ [R=301,L]
but the index.html redirect not working.
Does anyone know why? Thank you.

Use it like this:
RewriteEngine On
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=301,NC,NE]
# remove /lp/index.html
RewriteCond %{THE_REQUEST} \s/+lp/index\.html [NC]
RewriteRule ^ /lp/ [L,R=301,NE]
Clear your browser cache and retest.

try this
http://www.rapidtables.com/web/tools/redirect-generator.htm
with Apache .htaccess redirect option

Related

How to remove php extension form URL .htaccess

I have a code in my .htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
It removes .html extension from urls, how do I remove .php as well with the same code?
Check this modified rule
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.(php|html|htm)\ HTTP/
RewriteRule ^(.*)\.(php|html|htm)$ /$1 [R=301,L]
I am sure there's a shorter way to solve this out there but in this is how I remove php extension at the moment:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
Update:
Try this since you would like to main existing code
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.php\ HTTP/
RewriteRule ^(.*)\.php$ /$1 [R=301,L]

htaccess - redirect everything to index.php except a folder and specific files

I have been fighting with this for way too long. I have a my PHP site hosted with a .htaccess file for rewriting and redirecting. It has been working great so far. Now I simply want to add a subfolder, /pp that everything inside it does not get redirected or rewritten, basically it should not get touched by my .htaccess stuff.
Now when I go to mysite.com/pp/test.php (that file does exist), it redirects to mysite.com/index
Here is what I currently have:
RewriteEngine On
#remove www for all traffic
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#prevent redirect of submit
RewriteRule ^/?submit$ submit.php [L]
#prevent redirect of paypal
RewriteRule ^/?admin$ admin.php [L]
RewriteRule ^/?testPPButton$ testPPButton.php [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/pp/.* [NC] // this line is not working for some reason
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Options -Indexes
Updated .htaccess:
RewriteEngine On
# skip /pp/* from all rules below
RewriteRule ^pp(/.*)?$ - [L,NC]
#remove www for all traffic
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#prevent redirect of submit
RewriteRule ^/?submit$ submit.php [L]
#prevent redirect of paypal
RewriteRule ^/?admin$ admin.php [L]
RewriteRule ^/?testPPButton$ testPPButton.php [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Options -Indexes
Just below RewriteEngine On line add this to skip pp/ and everything under this folder:
RewriteEngine On
# skip /pp/* from all rules below
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+pp[/?\s] [NC]
RewriteRule ^ - [L,NC]
# rest of your rules go below this

Redirect index.php to root but not subfolders

I want to redirect www.domainname.com/index.php to www.domainname.com.
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
I used this code, but it redirects www.domainname.com/admin/index.php as well.
However, I don't want to redirect www.domainname.com/admin/index.php.
How can I achieve that?
You can use this rule:
RewriteCond %{THE_REQUEST} \s/+index\.php
RewriteRule ^index\.php$ / [R=301,L]

.HTACCESS Tricks - Rewriting for dynamic content

I need to be able to make it so that when someone visits my site at say:
a) http://www.mysite.com/article/this-article/
It actually displays the content from:
b) http://www.mysite.com/article/index.php?src=this-article
and also if a user types in (b) then they are 301'd to (a)
In my HT access I currently have the following:
<IfModule mod_rewrite.c>
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [R=301,L]
# force www.
rewritecond %{HTTP_HOST} ^mysite.com [nc]
rewriterule ^(.*)$ http://www.mysite.com/$1 [r=301,nc]
RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://www.mysite.com%{REQUEST_URI}? [R=301,L]
</IfModule>
I'm not sure what you're describing is possible using only rewrite rules. But you can use a combination of rewrite rules and PHP like so:
Change your .htaccess file to look like this (NOTE: This is assuming your .htaccess file is in your root)
<IfModule mod_rewrite.c>
RewriteEngine On
# force www.
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,NC]
RewriteCond %{REQUEST_URI} !^/article/index(-new)?\.php
# this is looking for anything past article that is not a /. You may
# need to change that to be more inclusive - something like (.*)
RewriteRule ^article/([^\/]+) /article/index-new.php?src=$1 [L,NC]
# index.php to /
RewriteRule ^index\.(php|html)$ / [R=301,L]
#RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
#RewriteRule (.*)index\.(php|html)$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://www.mysite.com%{REQUEST_URI}? [R=301,L]
</IfModule>
Your article/index.php will become a redirector to your new index.php, and would look something like this:
<?php
if (isset($_GET["src"]))
header("location: /article/" . basename($_GET["src"])); // Using basename() in case src happens to be referring to an actual file in your code
else
header("location: /");
?>
Your "real" index would actually be index-new.php, which is what the rewrite rule would be calling.

Apache .htaccess: How to remove question mark from URL if not `?id=(.*)`?

How to make .htaccess to remove question mark from URL if not ?id=(.*)?
# Rewrite for ?id=(.*)
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule .*$ %{REQUEST_URI}%1? [R=301,L]
# It does not work out on this way
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{QUERY_STRING} !^id=.*
RewriteRule .*$ %{REQUEST_URI}%1? [R=301,L]
This would be the right rule:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?#\ ]*)\?[^\ ]*\ HTTP/ [NC]
RewriteCond !{QUERY_STRING} id
RewriteRule .*$ %{REQUEST_URI}? [R=301,L]
Update:
# Query rewrite exceptions
RewriteCond %{QUERY_STRING} !callback=.*
Does this work?
RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} !^id=
RewriteRule ^(.*)$ $1?%1 [R=301,L]
Tip: during your testings, use 302 redirections instead of 301, as 301 redirections are stored by browsers. You can finally switch to classic 301 when you are done testing.