redirecting issue with .htaccess file in PHP - .htaccess

I have a site like https://yoursitename.com. I am using .htaccess file for redirection. The code in .htaccess file is as :
RewriteRule ^websites/all /filter.php?module=1&filter=all [R=301,NC,P]
But redirection goes me to another page which has the following htaccess code :
RewriteRule ^([a-zA-Z0-9_-]+)$ show-detail.php?c=$1
What I am doing wrong?
Content of .htaccess file
RewriteEngine on
RewriteBase /
RewriteRule ^websites/all /filter.php?module=1&filter=all [R=301,NC,P]
RewriteRule ^websites/established /filter.php?module=1&filter=established [R=301,NC,P]
RewriteRule ^websites/starter /filter.php?module=1&filter=starter [R=301,NC,P]
RewriteRule ^websites/active /filter.php?module=1&filter=active [R=301,NC,P]
RewriteRule ^websites/ending /filter.php?module=1&filter=ending [R=301,NC,P]
RewriteRule ^websites/justsold /filter.php?module=1&filter=justsold [R=301,NC,P]
RewriteRule ^websites/dealflow /filter.php?module=1&filter=dealflow [R=301,NC,P]
RewriteRule ^domains/all /filter.php?module=2&filter=all [R=301,NC,P]
RewriteRule ^domains/auction /filter.php?module=2&filter=auction [R=301,NC,P]
RewriteRule ^domains/active /filter.php?module=2&filter=active [R=301,NC,P]
RewriteRule ^domains/ending /filter.php?module=2&filter=ending [R=301,NC,P]
RewriteRule ^domains/justsold /filter.php?module=2&filter=justsold [R=301,NC,P]
RewriteRule ^domains/appraisal /filter.php?module=2&filter=appraisal [R=301,NC,P]
RewriteRule ^domains/brokered /filter.php?module=2&filter=brokered [R=301,NC,P]
RewriteRule ^apps/all /filter.php?module=3&filter=all [R=301,NC,P]
RewriteRule ^apps/ios /filter.php?module=3&filter=ios [R=301,NC,P]
RewriteRule ^apps/android /filter.php?module=3&filter=android [R=301,NC,P]
RewriteRule ^apps/active /filter.php?module=3&filter=active [R=301,NC,P]
RewriteRule ^apps/ending /filter.php?module=3&filter=ending [R=301,NC,P]
RewriteRule ^apps/justsold /filter.php?module=3&filter=justsold [R=301,NC,P]
RewriteRule ^(websites|domains|apps|digitalGoods|sell)($|/) - [L]
RewriteRule ^item/(.*)$ ./show-digital.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ show-detail.php?c=$1
RewriteCond %{HTTP_HOST} ^hiwebby\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.hiwebby\.com$
RewriteRule ^blog\/index\.php$ "http\:\/\/hiwebby\.com\/blog\/index\.php\/Support\/" [R=301,L]

You have to add the [L] Flag (Last) - so no other rules are checked
RewriteRule ^websites/all /filter.php?module=1&filter=all [R=301,NC,P,L]

Your rewrite rules seem dubious at many lines. In my answer, there
are few questions for you as well. Configuring server behavior with rewrite rules is tricky any misconfiguration will lead to the serious problem with the websites. That's why the complete lookup is necessary. Also, you should consider reading the document https://httpd.apache.org/docs/current/rewrite/flags.html#page-header
RewriteEngine on
RewriteBase /
That's fine.
RewriteRule ^websites/all /filter.php?module=1&filter=all [R=301,NC,P]
RewriteRule ^websites/established /filter.php?module=1&filter=established [R=301,NC,P]
RewriteRule ^websites/starter /filter.php?module=1&filter=starter [R=301,NC,P]
RewriteRule ^websites/active /filter.php?module=1&filter=active [R=301,NC,P]
RewriteRule ^websites/ending /filter.php?module=1&filter=ending [R=301,NC,P]
RewriteRule ^websites/justsold /filter.php?module=1&filter=justsold [R=301,NC,P]
RewriteRule ^websites/dealflow /filter.php?module=1&filter=dealflow [R=301,NC,P]
But in the code shown above:
Probably you are not using a proxy or redirect. So you can use flag [NC,L] instead. And in place of all the seven lines, you can use just
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^websites/(.*)$ filter.php?module=1&filter=$1 [NC,L]
Similarly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^domains/(.*)$ filter.php?module=2&filter=$1 [NC,L]
and
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^apps/(.*)$ filter.php?module=3&filter=$1 [NC,L]
so on (if there is any).
RewriteRule ^(websites|domains|apps|digitalGoods|sell)($|/) - [L]
In your above line how you are going to handle requests like domain.com/websites. Is there any directory named websites?.
The "-" syntax means that the requested URI is not modified.
RewriteRule ^item/(.*)$ ./show-digital.php?url=$1
In above code . is there for any purpose in ./show-digital.php?url=$1? and also there is no flag provided. You can modify it like
RewriteRule ^item/(.*)$ show-digital.php?url=$1 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ show-detail.php?c=$1 [NC,P]
Here also correct flag would be [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ show-detail.php?c=$1 [NC,L]
RewriteCond %{HTTP_HOST} ^hiwebby\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.hiwebby\.com$
RewriteRule ^blog\/index\.php$ "http\:\/\/hiwebby\.com\/blog\/index\.php\/Support\/" [R=301,L]
In this section can you explain what you want to do.

Related

Removing the .php file extension from the URL Request

I am trying to beautify my URLs and want to get rid of the .php file extension. I've found a couple of other questions here such as this one (Removing the PHP file type extension) But none of the suggestions work for me. They send me to my 404.php or do not change the URL at all.
I figure the entire RewriteEngine code in my htaccess as a whole is conflicting in itself.
Here's what I got so far,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [R]
</IfModule>
I would like my domains to look like
https://www.example.com/about
Don't mix Redirect, RedirectMatch and RewriteRule directives in same .htaccess as they come from different Apache modules and their load sequence is unpredictable.
Have it this way:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Make sure to clear your browser cache before testing this change.
Put in .htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
ErrorDocument 404 ./404
RewriteCond %{REQUEST_URI} ^404/$
RewriteRule ^(.*)$ ./404 [L]
Redirect 301 /index /
remove .php from the link :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Multiple rewrite rules and conditions in htaccess file

In my .htaccess file i have already some rewrite conditions and rules, and its working normal.
Now i need to add "http to https redirect" to my htaccess file.
Here is my old .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([a-z0-9_-]+)$ pages/$1.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2/$3.php [L]
I attempted to add below code to file but it doesnt work properly.
For example if i write the url direct to browser its work (only with www). But if click my any backlinks or google search result links it doesnt work.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
Where do i wrong? Where should i put the code? Any help would be great. Thanks.
your htaccess could look like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([a-z0-9_-]+)$ pages/$1.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2/$3.php [L]
At least, it works for me, in my own implementation
You can use the following htaccess :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)/?$ $1.php [L]
Clear your browser's cache before testing this

.htaccess redirect all index.html to parent folder

I need to redirect all index.html like this
Original URL
www.example.com/lp/index.html
www.example.com/sytem/index.html
Desired URL
www.example.com/lp
www.example.com/sytem
I used the follwing, it redirects successfully but 404 page
RewriteEngine On
RewriteRule ^index\.html$ [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
You almost got it right, what you did looks like a copy paste error to me. Your first RewriteRule is missing a parameter - there should be a slash before [R=301,L]
Your htaccess should look like this:
RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
Reference:
http://dense13.com/blog/2012/10/29/removing-index-html-with-mod_rewrite-in-htaccess/
Place this code in your DOCUMENT_ROOT/.htaccess file:
DirectoryIndex index.html
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^(.*?)index\.html$ /$1 [L,R=301,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html -f [NC]
RewriteRule ^(.+?)/?$ /$1/index.html [L]
Try putting this in your .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1/index\.html [PT,L]
PT and L are rewrite flags, for more details about them check http://httpd.apache.org/docs/2.2/rewrite/flags.html
why not redirect index.(anything) ? like index.php? index.xhtml?
RewriteEngine On
RewriteRule ^index\.(.*)$ / [R=301,L]
RewriteRule ^(.*)/index\.(.*)$ /$1/ [R=301,L]
or to base it from the initial URL request only (to avoid conflicts with other rewrite rules):
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.(.*) [NC]
RewriteRule ^(.*?)index\.(.*)$ /$1 [R=301,L]

htaccess issue when i try to access a directory

So here's my code:
RewriteEngine On
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^error$ error.php [QSA,L]
RewriteRule ^([^/.]+)/?$ product.php?weblink=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /error [L,R=301]
It's simple so when I try to access host/product-name it works but when I try to access for example a directory /blog or /blog/ redirect pe to error page specified by last rule. What's the problem?
You can do it this way
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^error$ error.php [QSA,L]
RewriteRule ^([^/.]+)/?$ product.php?weblink=$1 [L,QSA]
RewriteRule . /error [L,R=301]

.htaccess redirect index.php to /

I would like to hide the index.php page and just show the domain.
Is this possible with .htaccess?
RewriteRule ^index\.php/?$ / [L,R=301,NC]
Also tried:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.php HTTP/
RewriteRule ^index.php$ http://example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
index.php still shows
Try, It works for me! Make sure your have AllowOverride All set in httpd.conf
RewriteEngine On
RewriteCond %{REQUEST_URI} index\.php
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L]
There is a regex issue in your rules, I have modified your rules and it works for me:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php$ http://example\.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index\.php [L]
RewriteRule ^(.*)index\.(html|php)$ http://%{HTTP_HOST}/$1 [R=301,L]
You can rewrite '/index.php' through .htaccess like this:
# Remove /index.php from all urls
RewriteRule ^(.+)/index\.php$ /$1 [R=302,L]

Resources