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
Related
I need some help with editing .htaccess file.
need to remove trailing slash from URL, for example: website.com/file/ to website.com/file. Also redirect to website.com/file when typed website.com/file/
redirect to website.com/file, when user type website.com/file.php/ or website.com/file.php
resolve HTML anchor IDs like website.com/file.php#anchor to website.com/file#anchor or better website.com/file/anchor. Is that second possible?
I tried to remove .php extension from URL and now my CSS/JS files are not working. Is possible to use relative paths to link CSS/JS or just absolute?
My .htaccess file now looks like this:
RewriteEngine On
ErrorDocument 404 https://website.com/404
# Redirect to https
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ https%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove .php extension from URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Any ideas?
Are there some .htaccess "trics" used on websites?
Thanks!
This is my current .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# Force https
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Header set X-UA-Compatible "IE=Edge,chrome=1"
# block text files in the content folder from being accessed directly
RewriteRule ^content/(.*)\.(txt|md|mdown)$ error [R=301,L]
# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]
</IfModule>
What can I do to force a trailing slash when there isn't one? For instance, my current URLs look like this:
https://amemoirproject.com/chapters/quarter-after
I'd like to force a trailing slash so they look like this:
https://amemoirproject.com/chapters/quarter-after/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://%{HTTP_HOST}/$1/ [L,R=301]
The RewriteCond makes sure that there isn't any files with that name. If there is not, it adds the trailing slash.
I am trying to add some code to my .htaccess to redirect slash and non slash urls to the .html all url's apart from my homepage.
For example
www.mydomain.com/cat/ and www.mydomain.com/cat
should redirect to www.mydomain.com/cat.html
I have managed to add the following to my .htaccess which redirects www.mydomain.com/cat to the right place www.mydomain.com/cat.html but need some help on how to make slash version redirect to the .html page
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
My whole .htaccess looks like this, if anyone has any suggestions on how it should look in light of the above it would be greatly appreciated.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xxx.xxx.xxx.xx [nc,or]
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine on
# Pleas note that RewriteBase setting is obsolete use it only in case you experience some problems with SEO addon.
# Some hostings require RewriteBase to be uncommented
# Example:
# Your store url is http://www.yourcompany.com/store/cart
# So "RewriteBase" should be:
# RewriteBase /store/cart
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
</IfModule>
SOLVED:
I just added:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
The separate rules seems to be working for you, but I think you can simplify it to one rule, with an optional slash. Your rule redirects the slash to no-slash, which then redirects again to the .html. With one rule, you'd only have one redirect.
This has the standard RewriteCond that check if it's not a file or a folder, so it doesn't keep redirecting .html if it's already one. Then, the \? in the ReweriteRule is an optional slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://mydomain.com/$1.html [R=301,L]
If this is all in your domain, you can omit it from the result:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ /$1.html [R=301,L]
Also, note this will catch and work with subfolders, whether or not you mean it to. e.g.,
www.mydomain.com/animals/cat/ will redirect to www.mydomain.com/animals/cat.html
I want to make all my URLs uniformly clean. Which means all my URLs have no extensions, nor trailing slash, and if a person does enter in a .php or trailing slash, it would simply redirect the person to the clean URL.
Example:
example.com/blog/file.php
and
example.com/blog/file/
would both redirect to
example.com/blog/file
In .htaccess try this:
RewriteEngine on
#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]
My web host recently (Sept 2011) updated their Apache servers to a newer version of Apache, and my old .htaccess code to display extensionless URLs no longer worked. After a considerable amount of copying code (that probably worked with older versions of the Apache OS) and unsuccessfully testing, I was able to determine that the following code seems to work on my server, and several others:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.com/$1 [R=301,L]
Give that a try - the -Multiviews seemed to be the key element to get it to work.
# Turn off the need of directory slash redirecting
DirectorySlash Off
# Turn on RewriteEngine
RewriteEngine On
# Internally redirect request to php file
RewriteCond "%{REQUEST_FILENAME}" !-f
RewriteCond "%{REQUEST_FILENAME}\.php" -f
RewriteRule "(.*)" "$1.php" [L]
# External redirects
# The above line is used to determine whether a internal redirect is done or not.
# If it is internally redirected, REDIRECT_STATUS will become 200.
# RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
# Redirect request with .php extension to extensionless
RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
RewriteRule "(.*)\.php$" "$1" [R=301]
# Redirect request with trailing slash to slashless
RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
RewriteCond "%{REQUSET_FILENAME} -d
RewriteCond "%{REQUSET_FILENAME} !-f
RewriteRule "(.*)\/" "$1" [R=301]
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$