Hi! My english is not very good, sorry for any spelling mistakes.
Using .htaccess I want to add www. and remove/hide the .html.
It should look something like this:
BEFORE -- http://example.com/whatever.html
AFTER ----- http://www.example.com/whatever
I have .www adder:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1
And .html remover
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [N]
How to connect them?
NOTE: It want remove/hide .html from each .html file on my page.
I use this tool to test my .htaccess file: https://htaccess.madewithlove.be
Thanks for any help.
Could you please try following, based on your shown samples only. please make sure you clear your browser cache before testing your URLs. As per Mr.White's nice suggestion added condition in RewriteRule part to avoid passing css or js files to rule.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !\.\w{2,4}$ http://www.%{HTTP_HOST}%{REQUEST_URI}.html [L]
Related
I have a website of PHP files that I'm hosting on SiteGround.com and want the links to look like "example.com/about" instead of "example.com/about.php". I also want my index.php to be loaded when I visit "example.com" instead of "example.com/index"
So I did some searching and this is what I want:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
But the webserver already has a default .htaccess file which has this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule . /index.php [L]
</IfModule>
Okay...so I tried to piece together what it is doing. Based on my understanding, it enables the RewriteEngine, then sets RewriteBase to be /. It then sets a rule looking for index.php and if it finds it, it doesn't check any other rule.
Otherwise, it falls through and checks if a string is not a file and not a directory and contains a .php extension, it redirects the root to index.php and stops.
So, I tried adding my three lines to the end of block right before the closing </IfModule> tag and it didn't quite work. Can anyone help me out?
EDIT
I modified my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
In my HTML, I have:
<a href="about" .... ></a> <!-- link to about.php -->
<a href="/" ....></a> <!-- link to index.php -->
Is that right? It doesn't seem to work for me.
First of all, the line:
RewriteCond %{REQUEST_FILENAME}\.php -f
Is not part of the original .htaccess file. It can't be, it makes no sense where it is, perhaps you added it yourself while experimenting.
The .htaccess file you have there, other than that line, is a standard method of putting everything through index.php. Probably for WordPress. So you need to decide what you want to do. Do you want to put everything through index.php, or do you want to use separate .php files with the extension added on the server side? Or do you want to do both? (in which case a combined approach needs writing) Are you still using WordPress? Perhaps you can just remove those rules.
Let me know and I can update the answer if you need more info.
Update
Your rules can be modified to:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,NE]
That should fix it, I think the \. on the REQUEST_FILENAME line was breaking it, the rest are improvements.
If that's not working then perhaps mod_rewrite isn't enabled, or .htaccess files are not enabled. You can test it by putting some garbage on a line and see if you get a 500 error.
If the root doesn't serve index.php then you just need to add a DirectoryIndex.
I'm a beginner using htaccess and I don't know how to do what I want. I'd like to understand what I'm doing so I'd really appreciate if you can help me give me some advices for ver (very!) beginners... :)
I'd like to:
Redirect xxx.php, xxx.html or any extension to xxx (without extension)
Now, my htaccess is
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
and it works but just if I write xxx. But if I write xxx.php (I see the page :(, I'd like to redirect to xxx) and if I write xxx.html it doesn't show nothing.
Finally. I've like to rewrite variables to friendly links
i.e. If I have xxx.php?id=1 > I would like to redirect to xxx/username
Thank you in advance
Best wishes and merry christmas! :)
Try this:
I use this code already.
Rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
Put this in your htaccess file && then check for files without extension.
Thanks
Try adding these rules to your htaccess file:
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.php
RewriteRule ^ /%1 [L,R=301]
If you need to do the same with with .html extensions, then change the php part of that condition to (php|html?).
I need an .htaccess file which will do something like this:
When user input www.site.com/some_folder/test it will keep the address as it is and open the corresponding page (test.php)
When user input www.site.com/some_folder/test.php it will remove .php extension to www.site.com/some_folder/test and it will open the corresponding page (test.php)
What I have right now is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
and it can only fulfill first scenario but not the second. (when I input www.site.com/some_folder/test.php it will still show www.site.com/some_folder/test.php)
What should I change / add in the .htaccess file to fulfill 2nd scenario ?
Any help will be very much appreciated.
Thanks very much.
add the following directives to your .htaccess:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule (.*)\.php$ /$1 [R=301,L,QSA,NC]
Edit:
if you're in Localhost :
RewriteBase /sitename/
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule (.*)\.php$ $1 [R=301,L,QSA,NC]
I have no experience with .htaccess, but I got a tip that it's very useful so I wanted to try this.
I now have a file called .htaccess, in my root folder.
The files contains this;
RewriteBase /
RewriteEngine on
RewriteCond %{HTTP_HOST} ^kellyvuijst\.nl [nc]
RewriteRule (.*) http://www.kellyvuijst.nl/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
What I'm trying to do here is create a 'www.mysite.com/portfolio/' instead of 'mysite.com/portfolio.html' I used some tutorials on this and I think it's correct, but I'm not sure.
So now I have this file, and what now? The tutorials all show what to put in the file but not what to do with it? Do I need to call for it in every .html page I have? And how do I call for it?
A .htaccess file is automatically invoked by the server.
You have just to put this into your file :
RewriteBase /
RewriteEngine on
RewriteRule www.mysite.com/portfolio/ /mysite.com/portfolio.html [L]
Hmm, you're using a lot of rules here to achieve just that.
Anyway, no you don't have to include that file. If you're hosting your site on a server with Apache it'll be included automatically. Can you also run PHP files or is your site just HTML? That's always an easy sign if you're also using Apache (not 100%, but often the go together).
If so, you could try just using these rules first:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.+)\.(.+)$ [nc]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
If that always adds www to your address, even if you type in the URL without www at least you can be certain that it works.
Then, to make the .html disappear you can add this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule $(.*)/$ /$1.html [L]
This should make every url that ends with a slash (like portfolio/) use a .html file instead (portfolio.html), but only if /portfolio/ isn't an actual directory on your website.
(I removed your url from the rules because this way it should also work if you use it on another website, or if you change your url. It should still do what you want)
Made sure the server is configured to allow htaccess files to override host options. So in your vhost/server config, you need:
AllowOverride All
I have 2 questions.
I am currently using wamp server to serve my website.
The homepage is 'localhost/prefix/index.php'
Question 1:
a. I would like it so my home page is:
'localhost/prefix/'
instead of
'localhost/prefix/index.php
b. I would like it so:
'localhost/prefix/admin/profile.php'
is
'localhost/prefix/admin/profile'
How do I go about doing this (I have googled and I am very confused by the syntax)?
Question 2
If I have a url like
'localhost/prefix/games?title=hi'
how can I make it so the url is like this:
'localhost/prefix/games/hi'
Thanks in advance!!
I really have got lost.
EDITED::///
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [R]
Is what I have so far.. It does nothing... But everyone says it should! (the htaccess file is doing something because if I do something random, it throws up errors).
EDITED::///
This seems to remove .php and index.php from the url:
RewriteEngine On
RewriteBase /prefix/
# remove .php; use THE_REQUEST to prevent infinite loops
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]
Problem now is that my prefix base is not working so it ends up going to
localhost/something/something
rather than
localhost/prefix/something/something
Any ideas?
EDITED::///
I have sussed out that the above code actually works perfectly if the page i'm directing to is in a sub folder. so for example.. this will work:
localhost/prefix/admin/dashboard
But this (because the file is in the root directory, doesn't)
localhost/prefix/login.php
it redirects me to
localhost/login
Any ideas?
EDIT::///
If you are having problems getting it to work. close your browser down and restart... I had caching issues.
This code above will remove .php and also remove index.php.