Reputation:
I currently have the following .htaccess snippet, I don't understand it at all, and I am trying to simply fix few problems:
1 - How to make www.domain.co.uk and domain.co.uk and both with /index.php at the end to re-direct to the home page? as I have heard it is not good for SEO.
2 - I want to remove the .php that is after any URL (This already been done, but I don't know which line is for it in the above snippet).
3 - I have a database that I add projects to, and it generate the URL for projects that I add one of the URLs is "http://www.domain.co.uk/projects.php?project=Websites" how to get rid of ".php?project=" and make it look like www.domain.co.uk/projects/Websites
DirectoryIndex index.html index.php
ErrorDocument 404 http://www.domain.co.uk/404.php
ErrorDocument 504 /504.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.co\.uk$
RewriteEngine On
RewriteBase /
# remove enter code here.php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{HTTP_HOST} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,L]
NEW - We are working on it from the comments and answers below:
DirectoryIndex index.html index.php
ErrorDocument 404 /404
ErrorDocument 504 /504
RewriteEngine On
RewriteBase /
# remove enter code here.php; use THE_REQUEST to prevent infinite loops
# By puting the L-flag here, the request gets redirected immediately
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301,L]
# remove index
# By puting the L-flag here, the request gets redirected immediately
# The trailing slash is removed in a next request, so be efficient and
# dont put it on there at all
RewriteRule (.*)/index$ $1 [R=301,L]
# remove slash if not directory
# By puting the L-flag here, the request gets redirected immediately
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301,L]
# add .php to access file, but don't redirect
# On some hosts RewriteCond %{REQUEST_FILENAME}.php -f will be true, even if
# no such file exists. Be safe and add an extra condition
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !(/|\.php)$
RewriteRule (.*) $1\.php [L]
#Hey, this one is fine ;-)
RewriteCond %{HTTP_HOST} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,L]
Upvotes: 2
Views: 126
Reputation: 20737
Your code is certainly interesting. The rules you have would work fine on their own, but create a mess when used together. The problem is that you are using the R
flag without the L
flag. You are working with a mix of full url's and normal url's.
A request to example.com/otherpage.php
ends up being rewritten to:
1. 301: http://www.example.com/http://example.com/home/yourhome/public_html/otherpage
2. 302: http://www.example.com/404.php
3. 301: http://www.example.com/404
4. 200: (and finally this page gives a 200 - OK status code)
A request to example.com/index.php
follows an even weirder pattern:
1. 301: http://www.example.com/http://example.com/home/yourhome/public_html/
2. 301: http://www.example.com/http:/example.com/home/yourhome/public_html
3. 302: http://www.example.com/404.php
4. 301: http://www.example.com/404
5. 200: (and finally this page gives a 200 - OK status code)
So how to fix this? See the following .htaccess
(put comments inside for easier understanding)
DirectoryIndex index.html index.php
#/404.php redirects to /404, so why not do this immediately?
ErrorDocument 404 /404
ErrorDocument 504 /504
#These do nothing useful, so remove them
#RewriteEngine on
#RewriteCond %{HTTP_HOST} ^loaidesign\.co\.uk$ [OR]
#RewriteCond %{HTTP_HOST} ^www\.loaidesign\.co\.uk$
RewriteEngine On
RewriteBase /
# remove enter code here.php; use THE_REQUEST to prevent infinite loops
# By puting the L-flag here, the request gets redirected immediately
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301,L]
# remove index
# By puting the L-flag here, the request gets redirected immediately
# The trailing slash is removed in a next request, so be efficient and
# dont put it on there at all
RewriteRule (.*)/index$ $1 [R=301,L]
# remove slash if not directory
# By puting the L-flag here, the request gets redirected immediately
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301,L]
# add .php to access file, but don't redirect
# On some hosts RewriteCond %{REQUEST_FILENAME}.php -f will be true, even if
# no such file exists. Be safe and add an extra condition
# There is no point in escaping a dot in a string
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !(/|\.php)$
RewriteRule (.*) $1.php [L]
#The regex part of the rewritecondition must escape the dots.
RewriteCond %{HTTP_HOST} ^loaidesign\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.loaidesign.co.uk/$1 [R=301,L]
Make sure your /404.php
sends out an actual 404 Not Found header. The 200 OK
header states to, for example a search engine, that the page exists. The same is true for your /504.php
page. Besides that your 504.php gives several warnings.
Upvotes: 1
Reputation: 2130
Change (new code)
RewriteCond %{HTTP_HOST} ^loaidesign\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.loaidesign\.co\.uk$
to
RewriteBase /
(i.e., partially restore the original). Those two RewriteConds are doing nothing.
For (1), your old last two lines are correct to add www. to any domain name missing it:
RewriteCond %{HTTP_HOST} ^loaidesign\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.loaidesign.co.uk/$1 [R=301,L]
For (2), you want a more SEO/SEF URL, without the .php? You remove the .php elsewhere, such as your links or what the user types in, and add .php back in here in .htaccess, so that the server then works with filename.php. Remove
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
and replace it with
# add .php to any suspected file lacking one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
Note that this is rewriting the URI... it will not show up in the address bar, since there is no 30x return code.
Number (3) is standard SEO coding. First, your links must produce it in that format /projects/Wix_Websites
-- .htaccess does not do that. In .htaccess, you convert it back into a form that the server understands: /projects.php?project=Wix_Websites
. The question is: how general do you want to make this? Do you want any /XX/AA
to become XX.php?YY=AA
, or just specific ones like projects?
The "remove slash if not directory" I think will work, as is, if you want to put that back in.
Upvotes: 0
Reputation: 3540
This is not bad for SEO. And if you don't want /index.php to your url just don't link it that way. Remove /index.php from your links.
These lines adds ".php" to a file request. So if you link http://www.loaidesign.co.uk/somefile the server will request http://www.loaidesign.co.uk/somefile.php. It may also redirect somefile.css to somefile.css.php, but only if somefile.css.php exists.
# add .php to access file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
Rewrite your URL generation code to output links on the format you want (www.loaidesign.co.uk/projects/Wix_Websites) and add this to your .htaccess
RewriteRule ^projects/(.*)$ projects.php?project=$1 [L]
Then you can access your _GET as usual ($_GET['project'] will be equal to Wix_Websites in the obove example)
Upvotes: 2