On my site, all pages have .html extension and are directly off the root (i.e., site.com/page.html). I've successfully used the following .htaccess code to make .html not show:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
This gives me a url like site.com/page, but in the event that a user links to or visits the url using a slash (i.e., site.com/page/), I'd like to 301 redirect to the non-slash version.
I'm having trouble integrating this part into the above code. Thanks in advance for any suggestions.
Try to something like this
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]
Related
i've never really played around with htaccess before and can't figure out how I would go about displaying a html file in a folder called "radio" when someone was to go to mydomain/radio. The current code in htaccess is below and came with my website cms when I set it up. Any help is appreciated.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([^.]+)\.(png|jpg|gif|css|js|otf|ico) [NC]
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) public/$1 [L]
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^ public/index.php [QSA,L]
Hi i'm trying to add a trailing "/" to my website for the past few weeks and every code and website tutorial to edit the .htaccess i've tried has failed to work. All my webpages end with ".html". The first part of my htaccess code forces the use of "https" and "www". What I want is that + when a user clicks on a link on my site to say "a href="help.html" it will load help.html but it will appear in the address bar as "https://www.example.com/help/" I don't care if I need to change all my links to omit the ".html" or if I need to change all my filenames to omit the ".html" extension. I just want that trailing "/" + my www and https force. Here is my code so far:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=302]
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=302,L]
P.S. I am aware of the 302 redirect as i'm testing, once my code is working i'll change to 301. Any help would be appreciated.
Add this and If the URL doesnt have a trailing slash the htaccess will redirect the browser to the URL with a the slash.
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
I have a webpage, which has got a html page called "blog.html" and a subfolder which is called "blog" and have some other html page inside. I just removed .html with htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Now, when somebody click on the "blog" menu, the following happened:
You don't have permission to access /blog/.html on this server.
I wonder how can i fix this, i would like to have a page with working urls like this:
mydomain.com
mydomain.com/blog
mydomain.com/blog/first_post
Change your rule to this:
DirectorySlash Off
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
And test it in a different browser.
I have website that has links like
domain-name.com/pages/contact-us/
www.domain-name.com/pages/contact-us/
domain-name.com/pages/about-us/
www.domain-name.com/pages/about-us/
and other pages ...
What I'm looking for how i can redirect them permanently to same subfoldername.html using htaccess . for example for all the links above should redirected to
domain-name.com/contact-us.html
www.domain-name.com/contact-us.html
domain-name.com/about-us.html
www.domain-name.com/about-us.html
also for other pages, otherwise if page not found to root index.html .
Thanks in advance.
Try the following
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(/pages/(.*[^/])/?)$
RewriteRule ^(.*)$ /%2.html [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.html [L,R=302]
The first three lines will remove the folder pages. If there is a trailing slash, it will be removed. The last three lines will redirect to /index.html, if the request file does not exist.
I am using this htaccess file to re-write my urls:
RewriteEngine On
# Friend SEO url
#RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.php
#RewriteRule ^ /%2/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
The url rewriting works perfectly fine (for example, mypage/web-store/ can be used instead of mypage/web-store.html).
The problem however, is that my rewritten url can't retrieve the CSS file correctly. If I use the normal url however, I have no problem at all because the CSS file is in a subfolder of mypage. But with the url rewrite, it looks for the css file inside the folder /web-store/, which doesn't exist...
Anyone knows a way to counter this ?
You need to either change your URLs to absolute URLs (add a leading /) or you can set a relative URL base in the header of your pages:
<base href="http://example.com/mypage/" />
Where "example.com" is the domain name of your site and the /mypage/ is where the css is at.
If all else still fails, you can try to rewrite the proper page:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+\.css)$ /path/to/styles/$1 [L,R=301]