.htaccess not redirecting home page - php

Here is my .htaccess on a Linux system:
ErrorDocument 401 ./error/
ErrorDocument 403 ./error/
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.website.co.uk/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.website.co.uk/$1 [R=301,L]
Everything works as it should; www is added to non-www requests and a trailing slash is added. However when visiting www.website.com (which is added as a parked domain on cPanel) the user is NOT redirected to www.website.co.uk
If the visit website.com (note no www) then they ARE redirected.
What do I need to add/change in .htaccess?

Have your rules like this:
ErrorDocument 401 ./error/
ErrorDocument 403 ./error/
RewriteEngine on
RewriteBase /
# if not www.website.co.uk then redirect to it
RewriteCond %{HTTP_HOST} !^www\.website\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.website.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://www.website.co.uk/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]

Related

Error document 404 "message" directive not working in .htaccess

I do anything for a few days, .htaccess 404 does not work. I tried hundreds of methods, but I did not get it again
this my httaccess :
RewriteEngine on
ErrorDocument 404 "test comment"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://spahan.ir/$1 [R=301,L]
RewriteCond %{HTTPS_HOST} ^www\.spahan\.ir$
RewriteRule ^/?$ "https\:\/\/spahan\.ir\/" [R=301,L]
RewriteCond %{HTTPS_HOST} ^spahan.ir [NC]
RewriteRule ^(.*)$ http://www.spahan.ir/$1 [L,R=301,NC]

Nice url with htaccess with specific parameters

I want to have this form of URL : https://domain(:port)/lang/x/y/z/other
With this htaccess file, I can't achieve exactly what I want.
If I enter the URL https://localhost:port/lang/, I lose the port.
If I enter the URL https://localhost:port/lang, i got the URL https://localhost:port/langx/y/z
Please help me!
Options +FollowSymLinks
RewriteEngine On
#LIVE
RewriteBase /
#--------------------------------------
#-- 404 & 500 Redirects
#--------------------------------------
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
#--------------------------------------
#-- Force HTTPS
#--------------------------------------
RewriteCond %{HTTP_HOST} !=localhost$ [NC]
RewriteCond %{SERVER_PORT} !=8088
RewriteCond %{SERVER_PORT} !=8080
RewriteCond %{HTTPS} !=on
#RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^(actions)($|/) - [L]
#--------------------------------------
#-- Add trailing PHP and Params
#--------------------------------------
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.php?param1=$2&param2=$3&param3=$4&param4=$5 [B,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ $1.php?param1=$2&param2=$3&param3=$4 [B,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?param1=$2&param2=$3 [B,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#check for the existance of a PHP file first or else the 404 won't work
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)$ $1.php?param1=$2 [B,QSA,L]
RewriteRule ^(.*)/$ $1 [R=301,L]

Redirect entire www url to non www url

I'm using the following code in htaccess to redirect users from www to non www url of my site.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
#Force non-www:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Right now if someone lands on one of my web pages of my site with www. it redirects them to the main page with a non www.
I want it to redirect them to the same page they were on with it being non www.

How to redirect requests with .php (extension)

I've found a partial answer here.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php - [F,L]
[works] www.example.com/file.php to redirect to 404
[works] www.example.com/file to serve under /file.php (without showing on address bar)
[works] www.example.com/folder/ to NOT redirect to 404
[does not work] www.example.com/folder/index.php to redirect to 404
I'm aware that www.example.com/folder/ is served as www.example.com/folder/index.php (without showing on address bar) because of DirectoryIndex
I've tried playing around and disabling DirectoryIndex but I just couldn't figure it out.
What I have so far:
# if file has a .php extension redirect to 404 page #
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteCond %{THE_REQUEST} !^[A-Z]{3,}\s/+index\.php(/[^\s\?]+)? [NC]
RewriteCond %{REQUEST_URI} !(.*)/index\.php$
RewriteRule ^(.*)$ 404.php [L]
# this makes files work without .php #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]
Try this,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourfolder/index.php?/$1 [L]
</IfModule>
Try this code:
# if file has a .php extension redirect to 404 page #
RewriteCond %{THE_REQUEST} \s.+?\.php[\s?] [NC]
RewriteCond %{REQUEST_URI} !/index\.php [NC]
RewriteRule ^ 404.php [L]
# this makes files work without .php #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]

Apache mod rewrite like twitter url

Here is my .htaccess code
ErrorDocument 404 /404.php
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ profile?username=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php
RewriteRule ^s/([^/]*)$ /s.php?share=$1 [L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I would users on my website to be able to access their profile like how twitter does. For instance www.mywebsite.com/Jim would actually be rewritten as www.mywebsite.com/profile?username=Jim.
Currently the only part of the code that works is allowing url without the php extension. For instance www.mywebsite.com/home instead of www.mywebsite.com/home.php.
Moreover, the rewrite rule for s.php does not work like it should. I would like the page www.mywebsite.com/s/123456 to be written as www.mywebsite.com/s?share=12345.
I don't if the order of my htaccess is wrong but none of these seem to work. Additionally when a page is not found I'm still getting the default Apache 404 even though I have a rewrite rule to 404.php
Your redirect needs to be first, then your profile, then your share, then the most generalized php extension:
ErrorDocument 404 /404.php
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ profile.php?username=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^s/([^/]*)$ /s.php?share=$1 [L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]

Categories