I have one website running in that we need to modify .htaccess. There has been the error reported by Google that URL is being appended by one more PHP file.
Such as http://www.example.com/test-file.php/test1-file.php
We need to either remove test1-file.php or redirect to the 404.php page using .htaccess
My .htaccess looks like below
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/
RewriteRule ^(.*)/$ http://www.example.com/$1 [R=301,L
<IfModule mod_rewrite.c>
RewriteEngine on
ErrorDocument 404 http://example.com/404.php
</IfModule>
You may use these rules:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}${REQUEST_URI} [L,R=301,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [R=301,L,NE]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=302,L]
# remove content after 2nd slash
RewriteRule ^(.+\.php)/.*$ /$1 [L,NC,R=301]
Related
Basically what I want to do is :
1) redirect any .html request to .php page.
2) if any 404 happens , then it should redirect to www.domain.com
3) domain.com should be redirected to www.domain.com
4) www.domain.com/index.html should redirect to www.domain.com
Here is my htaccess code
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^fr/(.)*$ / [R=301,NC,L] # Added line
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . http://www.domsdain.com/index.php [L]
</IfModule>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,NC,L]
With above code 4th rule (www.domain.com/index.html should redirect to www.domain.com) is not working
You can refactor to use these rules:
ErrorDocument 404 http://www.examle.com/
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+index\.(?:php|html) [NC]
RewriteRule ^ http://www.examle.com/ [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(.+)\.html$ $1.php [L,NC]
RewriteRule ^fr/(.)*$ / [R=301,NC,L]
Make sure to clear your browser cache or use a new browser for testing.
I have this htaccess and it almost is working, but..
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.loggs\.no [NC]
RewriteRule ^(.*)$ https://loggs.no/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \s/+show(?:\.php)?\?id=([0-9]+) [NC]
RewriteRule ^ show/%1? [R,L]
RewriteRule ^show/([0-9]+)/?$ show.php?id=$1 [L,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
But it doesn't add trailing slashes to all my pages and when I'm clicking on show/6 and then will go to e.g. Show/3 it goes until show/show/3
and I would like to remove .php for all my pages too.
You can use these rules in site root .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(loggs\.no)$ [NC]
RewriteRule ^(.*?)/?$ https://%1/$1/ [L,R=301,NE]
RewriteCond %{THE_REQUEST} \s/+(show|feeds|links)(?:\.php)?\?id=([0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,NE,L]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,NE,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
RewriteRule ^(show|feeds|links)/([0-9]+)/?$ $1.php?id=$1 [L,QSA,NC]
RewriteRule ^(show|feeds|links)/?$ $1.php [L,NC]
I've read many articles and cannot seem to get ALL of the combined .htaccess Rewrites to work together. I either get re-direct loops or one or a few do not work at all.
To be clear I'm looking for the following 5 to occur if needed:
Ensure www on all URLs
Ensure HTTPS for all version of site
Remove index.php from url
Remove all .php extension / Re-direct to url without .php extension
Could be in tandem with the previous: add trailing slash
Some examples:
example.com/index.php => https://www.example.com/
www.example.com/info/really-good-info.php => https://www.example.com/info/really-good-info/
www.example.com/about-us.php => https://www.example.com/about-us/
www.example.com/careers/index.php => https://www.example.com/careers/
Here is current .htaccess setup:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.(?!js|css)([^.]*)$ $1\.php
# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
# Ensure www on all URLs.
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Ensure all URLs have a trailing slash.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://www.example.com/$1/ [L,R=301]
</IfModule>
The above .htaccess is ONLY in my root folder and currently does 3 out of the 5 needed: changes to HTTPS, adds www and removes index.php. It does not remove any other .php files extension nor does it add trailing slash.
I see 2 issues:
Redirect rules appearing after rewrite ones
Adding .php should only happen after you ensure corresponding .php file exists.
Have it this way:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# Ensure www on all URLs.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=302]
# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteCond %{THE_REQUEST} \s/*(.*?)/index\.php [NC]
RewriteRule ^ %1/ [R=302,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
# Ensure all URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]*?[^/.]$ %{REQUEST_URI}/ [L,R=302]
# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ index.php?$1 [L,QSA]
Make sure to clear your browser cache before testing this.
Try this to avoid the loop:
#non-www. http to www. https
RewriteCond %{ENV:HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
#non-www. https to www. https
RewriteCond %{ENV:HTTPS} on
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/*(.*?)/index\.php [NC]
RewriteRule ^ %1/ [R=302,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
# Ensure all URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]*?[^/.]$ %{REQUEST_URI}/ [L,R=302]
# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ index.php?$1 [L,QSA]
I have an issue I can't seem to debug. I have tried many solutions and no luck.
When I access a URL with subfolder/index.php I get redirected back to the root page. If I access another page in that subfolder it works fine. I am not sure what is causing this on my code. Here is what I have.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.domain\.com
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]
Any suggestions? Thanks!
Keep your rules like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^www.domain\.com
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule ^(.+?)/$ $1 [R=301,L,NE]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]
I have tried everything from forums but no result.
How can I hide directory "example" from all url's in website.
For example when user enters:
www.domain.com/example/en/file.php or
www.domain.com/example/ru/file.php or
www.domain.com/example/de/file.php
I want him to see in the url only:
www.domain.com/en/file.php or
www.domain.com/ru/file.php or
www.domain.com/de/file.php
but conent have to be taken from
www.domain.com/example/en/file.php or
www.domain.com/example/ru/file.php or
www.domain.com/example/de/file.php
My current htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
#RewriteCond %{THE_REQUEST} /example/ [NC]
#RewriteRule ^example/(.*)$ /$1 [L,R=301,NC,NE]
#RewriteRule ^((?!example/).*)$ /example/$1 [L,NC]
NOTE:
it can be more directories in "example" than en, ru or de.
put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# remove /example/ from URLs
RewriteCond %{THE_REQUEST} /example/ [NC]
RewriteRule ^example/(.*)$ /$1 [L,R=301,NC,NE]
# add www to host name
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# other than /js/ internally prefix /example/ before all URIs
RewriteCond $1 !js [NC]
RewriteRule ^((?!example/)[a-z]{2}/.*)$ /example/$1 [L,NC]
# if a matching .php file is found add .php internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
you can just put this line
Options -Indexes