I have this htaccess:
RewriteEngine On
# redirect with www
RewriteCond %{HTTP_HOST} ^mydomain [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [R=301,L]
# add .php internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
So my .php files can be called without the .php extension.
But I'd like them to be called only with a trailing slash. So when this trailing slash is not given, it should be appended with a 301. The problem I have is that this is giving me problems with the initial www, and the .php extension itself (sometimes it is adding recursively .php).
How can it be done?
Thanks!
I think you need to add something like this before your last rewrite rule, to avoid rewriting URIs that already end in .php
RewriteCond %{REQUEST_URI} !\.php$
Related
I've modified my .htaccess file to force trailing slashes on all the pages but I'm wondering how to remove the trailing slash in a particular url page? Need help. For example:
abc.com/test successfully redirects to abc.com/test/
But I want to remove that force trailing slash in a particular url of the site,
abc.com/demo/ should redirect to abc.com/demo
Here what I have done so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/demo/
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
Update:
To remove trailing slashes for multiple urls what would be the code, eg abc.com/demo2/ abc.com/demo3/ abc.com/demo4/ etc.. Any suggestions?
Try :
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/demo/ [NC]
RewriteRule ^demo/$ /demo [L,R]
RewriteCond %{REQUEST_URI} !^/demo [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
I am writing code in htaccess to add trailing slashes in url, also applying redirection.
My code is-
#Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
Redirect 301 /social-media-consultant http://example.com/seo-consultant/
Redirect 301 /uiux-developers http://example.com/graphics-designer/
Now, problem is that when I write code for adding slashes then redirection stops working.
Change order of your rules
Use only mod_rewrite based rules.
Have it this way:
RewriteEngine On
RewriteRule ^social-media-consultant$ http://example.com/seo-consultant/ [L,NC,NE,R=301]
RewriteRule ^uiux-developers$ http://example.com/graphics-designer/ [L,NC,NE,R=301]
#Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^ http://example.com/%{REQUEST_URI}/ [L,R=301,NE]
Make sure to clear your browser cache before testing this change.
I have the following code in my htaccess file:
Options -Indexes
RewriteEngine On
# Redirect external .php requests to extension less url
RewriteCond %{THE_REQUEST} ^(.+)\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}/foo/$1 [R=301,L]
# Resolve .html file for extension less html urls
RewriteRule ^([^/.]+)$ $1.html [L]
That work fine when I go to www.mydomain.com/foo/index whithout .html. I want force trailing Slash at the end of index but i don't know how
Does anyone know how to modify my code to make the trailing slash work ?
Thanks!
To add a trailing slash to your extension less html urls, You need to modify your both of the rules.
In the first rule, we add a trailing at the end of the target url so that a slash is added to all .html requests.
and in the second rule we add a trailing slash at the end of the regex pattern to accept a request with a trailing slash.
Options -Indexes
RewriteEngine On
# Redirect external .php requests to extension less url
RewriteCond %{THE_REQUEST} ^(.+)\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}/foo/$1/ [R=301,L]
# Resolve .html file for extension less html urls
RewriteRule ^([^/.]+)/?$ $1.html [L]
Clear your browser's before testing this!
(Hope,this helps!)
I modified my code for this:
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.html(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.html$ http://%{HTTP_HOST}/fr/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://%{HTTP_HOST}/fr/$1/ [R=301,L]
It works well
I am amending my htaccess file to achieve non-www to www (this is working) plus removing the trailing slash at the end of the URL, e.g.:
www.domain.bc.ca/club/ ---> www.domain.bc.ca/club
www.domain.bc.ca/club/index.html/ ---> www.domain.bc.ca/club/index.html
The portion of the htaccess file is below - the Force www bit is working; the Remove trailing slash bit is not. Help! Many thanks, Amanda.
# Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.bc\.ca$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]
I'm going to go out on a limb and guess that you're trying to access a directory when this happens. In your example, the "club" seems to be a directory and when you redirect /club/ to /club, a module called mod_dir will redirect it back to having the trailing slash again. There's a really good reason for this, because if the trailing slash is missing for a directory, the directory's contents will be displayed instead of the index file. That means if you were able to go to www.domain.com/club (without the trailing slash), you'd see all the contents of the club directory instead of the club/index.html file.
If that's ok with you, then you can turn off mod_dir by adding this to your htaccess file:
DirectorySlash Off
But then you'd need to internally add the slash back in:
DirectorySlash Off
# Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.bc\.ca$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]
# Add the slash back
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ /$1/ [L]
I'm trying to force a trailing slash to my URLs, but I can't make it work the way I want. This is my .htaccess file:
RewriteEngine on
#Force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
#Subdomains
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond $1/%1 !^([^/]+)/\1$
RewriteRule ^/([^/]+)? /%1%{REQUEST_URI} [L]
#Point everything to page.php
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond $1 !^(.*).(php|css|js|png|jpg|gif|htm|html)$
RewriteRule ^(.*)$ page.php?q=$1 [L,NC]
If I go to "en.example.com/about" I'm redirected to "en.example.com/en/about/", which is an invalid page.
How can I make this work?
The problem here is that the L flag causes a restart of the rewriting process with the rewritten URL (I’ve already told you that, didn’t I?):
Remember, however, that if the RewriteRule generates an internal redirect (which frequently occurs when rewriting in a per-directory context), this will reinject the request and will cause processing to be repeated starting from the first RewriteRule.
Now when /about is requested, the first rule get’s applied and redirects to /about/. The subsequent request of /about/ is then processed, at first the third rule is applied and the URL path is rewritten to /page.php. So far, so good.
But now the internal redirect takes place and the rewriting process is restarted with the new URL path /page.php. This is then fetched by the first rule again and redirected externally to /page.php/.
The second rule shouldn’t be applied at all as the pattern ^/ should never match as the per-directory path prefix is removed before testing the pattern when using mod_rewrite in an .htaccess file:
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done.
But these rules should work:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$ [NC]
RewriteCond %1 !=www [NC]
RewriteCond $0/%1 !^([^/]+)/\1$
RewriteRule ^[^/]* /%1%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} !=www.example.com [NC]
RewriteCond $1 !.*\.(php|css|js|png|jpg|gif|htm|html)$
RewriteRule .* page.php?q=$0 [L]