.htaccess: Remove extension and force traling slash (seo friendly) - apache

I've been playing about with my .htaccess file and so far that's doesn't work.
I'm trying to force the .www prefix, while removing the .html extension and adding/force a trailing slash at the end of URL.
Example (with test.html file):
http://www.domain.com/test.html
http://www.domain.com/test
http://domain.com/test.html
http://domain.com/test
= http://www.domain.com/test/
My .htaccess:
Options +FollowSymLinks +MultiViews
RewriteEngine on
Rewritecond %{HTTP_HOST} ^laforgenumerique.fr$
Rewriterule ^(.*) http://www.laforgenumerique.fr/$1 [QSA,L,R=301]
#REMOVE DOT HTML
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
#FORCE TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
#CUSTOM ERROR DOCUMENT
ErrorDocument 400 http://www.laforgenumerique.fr/404.html
ErrorDocument 401 http://www.laforgenumerique.fr/404.html
ErrorDocument 403 http://www.laforgenumerique.fr/404.html
ErrorDocument 404 http://www.laforgenumerique.fr/404.html
ErrorDocument 500 http://www.laforgenumerique.fr/404.html
Please help!
Host: OVH

You wrote:
#REMOVE DOT HTML
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
#FORCE TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
It seems that you are trying to mix two separate rules.
[L] suffix means that this is the last rule; all rules below will be skipped even if they are suitable.
You may try something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ www.$1
RewriteRule ^(.*).html$ $1/ [L]

Related

error 404 redirect not working with my .htaccess

I tried usingĀ 
ErrorDocument 404 /abc/404.php
But when the wrong URL is entered it redirects to the main page, not to the 404 error page
I have a htaccess file with this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?search=([^\s&]+) [NC]
RewriteRule ^ search/%1? [R=302,L,NE]
RewriteRule ^search/(.*)/page/(.*)/?$ search.php?search=$1&page=$2 [NC,L]
RewriteRule ^search/(.*)$ search.php?search=$1 [L,QSA,NC]
RewriteRule ^category/(.*)/page/(.*)/?$ category.php?id=$1&page=$2 [NC,L]
RewriteRule ^category/(.*)$ category.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
ErrorDocument 404 /abc/404.php
These two rules...
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
RewriteRule ^(.*)$ index.php/$1 [L]
Replace any URL that is accessed with the replacement... Unless the accessed URL is a real file/directory. As per the RewriteCond rules preceding them:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
If you don't want to divert every request that isn't a real file/directory then you need to change those rules...

Remove ?gclim=<random_code> using .htaccess

I have this .htaccess
RewriteEngine On
Options -Indexes
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www.****.com$
RewriteRule ^(.*)$ http://www.****.com/$1 [L,R=301]
RewriteCond $1 !^(index\.php|media|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
ErrorDocument 404 /page-not-found
And when google ads navigate to it because of the codeigniter setup it causes a 404, i need to remove the gclim from the URL completely.
How can i do that with pure .htaccess
I need to remove the gclim from the URL completely.
You can insert this redirect rule just below 2nd Options line:
ErrorDocument 404 /page-not-found
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&)?gclim=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [L,R=301,NE]
RewriteCond %{HTTP_HOST} !^www.****.com$
RewriteRule ^(.*)$ http://www.****.com/$1 [L,R=301]
RewriteCond $1 !^(index\.php|media|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

htaccess fails to redirect when encounter whitespace

I am using htaccess for clean url. Which is working fine as long as the parameters doesn't carry whitespace. In which case it converts the space in %20 and I receive 404 error.
So basically the URL: http://localhost:8080/series/dynamics/admin/cleanURL/green%20apple
gives me 404 error. But URL: http://localhost:8080/series/dynamics/admin/cleanURL/greenapple works fine.
Also is there a way I can remove the directory details from the URL, I tried
RewriteRule ^series/dynamics/admin/cleanURL/(.*)$ /$1 [L,NC,R]
But doesn't work
htaccess
<IfModule mod_rewrite.c>
Options +MultiViews
Rewriteengine on
RewriteBase /series/dynamics/admin/cleanURL/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^series/dynamics/admin/cleanURL/(.*)$ /$1 [L,NC,R]
Rewriterule ^([a-zA-Z0-9]+)/?$ index.php?product=$1
RewriteRule ^(.*)(\s|%20)(.*)$ /$3-$4 [R=301,L,NE]
#rewrite group and subgroup e.g. http://.../value1/value2/ [L,NC,QSA]
Rewriterule ^([a-zA-Z0-9]+(.*)+)/([^/]+(.*)+)/?$ index.php?product=$1&subgroup=$2 [L,NC,QSA]
</IfModule>
You will need to add this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) series/dynamics/admin/cleanURL/$1 [L]
Then have these rules in /series/dynamics/admin/cleanURL/.htaccess:
Options +MultiViews
Rewriteengine on
RewriteBase /series/dynamics/admin/cleanURL/
RewriteCond %{THE_REQUEST} /series/dynamics/admin/cleanURL/(\S*)\s [NC]
RewriteRule ^ /%1 [L,R=302,NE]
RewriteRule ^([^\s\x20]*)[\s\x20]+(.*)$ $1-$2 [L,R=302,NE]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
Rewriterule ^(\w+)/?$ index.php?product=$1 [L,QSA]
#rewrite group and subgroup e.g. http://.../value1/value2/ [L,NC,QSA]
Rewriterule ^([a-zA-Z0-9]+(.*))/([^/]+(.*))/?$ index.php?product=$1&subgroup=$2 [L,NC,QSA]

clean URLs with htaccess

I'm trying to have clean URLs, here is my code:
Options -Multiviews -Indexes +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteEngine ON
RewriteRule ^([0-9a-z\-\_]+)?$ profile.php?q=$1 [QSA,L,NC]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
ErrorDocument 404 /page/error?q=aqw
The only issue is that, i can not call a directory, i have to specify the file name, for exemple:
Help //this is not working
//This is working
So how can i fix that, thanks
Looking at your rules I think your first rule is redundant that you can remove.
Try this in your document root:
RewriteEngine On
RewriteBase /
ErrorDocument 404 /error?q=aqw
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

htaccess trailing slash not working

I have the below code in my htaccess. The site works fine (ie: no 500 errors) but it is not adding the trailing slash to my URLs:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://staging.foo.com/$1/ [L,R=301]
</IfModule>
What do I have wrong here?
You need to swap the order of your rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://staging.foo.com/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [NC,L]
</IfModule>
The reason why it wasn't working was because the routing to /index.php rule was being applied first, then the rewrite engine loops, and then the 2nd rule (the redirect rule) won't get applied because the URI has been rewritten to /index.php and that causes the %{REQUEST_FILENAME} !-f to fail, since index.php exists, thus, the redirect never happens.