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?).
Related
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]
So launched a site and made a mistake with a link and did not structure it correctly. As quite a few of these links have been picked up in Google I need to redirect the wrong links permanently without affecting the correct links and am not sure how to do it properly.
My links should be like this: /compare/itemname.html
But with the mistake they are appearing like this in Google:
/compare/itemname (without the .html part)
this was only because I missed off the .html when the links are created
dynamically and have fixed this bit now.
This is in my htaccess and works fine:
RewriteCond %{THE_REQUEST} \ /+index\.php\?keyword=([^&\ ]+)
RewriteRule ^ /compare/%1.html? [L,R]
RewriteRule ^compare/([^/]*)\.html$ /index.php?keyword=$1 [NC,L]
So how do I change from this /compare/itemname to /compare/itemname.html
where itemname can change - without messing up the above code in the
.htaccess
Or is it better at this stage just to do a 301 redirect and again how
would I pick out the urls itemname that is missing the .html ?
Thanks for any assistance
You can add a new redirect rule to add .html:
RewriteCond %{THE_REQUEST} \ /+index\.php\?keyword=([^&\ ]+)
RewriteRule ^ /compare/%1.html? [L,R]
RewriteRule ^(compare/[^.]+)/?$ /$1.html [NC,L,R]
RewriteRule ^compare/([^.]+)\.html$ /index.php?keyword=$1 [NC,L,QSA]
Try this .htaccess in your /compare/ dir
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*[^/])$ http://www.example.com/compare/$1\.html [L,R=301]
I want to use .htaccess rewrites to create the illusion of each user having their own folder on a website, when it's really just a handful of PHP files that generate all user content (often by pulling from a database):
/members/bob/avatar.jpeg points to /members/avatar.php?username=bob
/members/bob/about.html points to /members/about.php?username=bob
I'm completely lost on how to do this... I've pulled up tutorials on using the htaccess rewrite mechanics, but none of them involve changing /folder_name/variable/file_synonym to /folder_name/file_actual?var=variable. If anyone knows how to do something like that I'd be really interested, because I have no idea where to even start on this.
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+members/avatar\.php\?username=([^&\ ]+)
RewriteRule ^ /members/%1/avatar.jpeg? [L,R]
RewriteCond %{THE_REQUEST} \ /+members/about\.php\?username=([^&\ ]+)
RewriteRule ^ /members/%1/about.html? [L,R]
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^members/([^/]+)/(avatar|about)\.(jpeg|html)$ /members/$2.php?username=$1 [L]
This ended up being the solution:
RewriteEngine On
RewriteRule ^members/([A-Za-z0-9-]+)/avatar.jpeg?$ members/avatar.php?username=$1 [NC,L]
RewriteRule ^members/([A-Za-z0-9-]+)/about.html?$ members/about.php?username=$1 [NC,L]
RewriteRule ^members/([A-Za-z0-9-]+)/feed.html?$ members/feed.php?username=$1 [NC,L]
I used http://htaccess.madewithlove.be/ to test my work as I went along, to make sure that I was doing everything right. Apparently internal htaccess rewrites aren't allowed on Hostgator's shared servers, so I'll have to wait until I upgrade to really use this method.
Looking for some help with my .htaccess file and mod-rewrite.
I want to make my URLs more friendly.
I would like to take my URLS from:
www.site.com/contact.php
to:
www.site.com/contact/
Any help would be much appreciated?
As you've already mentioned, you need to add an entry to the htaccess.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^contact /contact.php
Or if you want to do this to all of your php pages then just modify it as following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Currently, my .htaccess file looks like htis:
IndexIgnore */*
RewriteEngine on
RewriteRule ^add /add.php
RewriteRule ^add/$ add.php
RewriteRule ^sitemap.xml /xmlsitemap.php
RewriteRule ^([^/\.]+)$ /index.php?slug=$1
RewriteRule ^([^/\.]+)/$ /index.php?slug=$1
It works fine for links as: site.com/category/
However, i would like the complete slug after site.com/. So that i can redirect site.com/category1/subcategory2/subsubcategory3 etc. There can be an unknown amount of subcategories inside a category.
I tried with request_uri but that didn't really work out.
How to do this? Thanks a bunch!
EDIT:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* slug.php?%{QUERY_STRING} [L]
this will pass URL to slug.php
ok, got it. it's simply: deleting the ^/. from the regex. i just figured that means that it has to stop at any / :) but... thx! it works now! :)