using htaccess to fix url mistake - .htaccess

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]

Related

how to hide id = from url using htaccess file

I need the url
from
localhost/project/category?c=electronics
to
localhost/project/category/electronics
I have tried
RewriteRule ^category/([^/\.]+)?$ /category.php?c=$1 [L]
RewriteRule ^category/+?$ /category.php?c=$1 [NC,L]
With your shown samples and attempts please try following htaccess rules. Please do clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
##External redirect to url change in browser.
RewriteCond %{THE_REQUEST} \s/(project/category)\.php\?c=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite to category.php in backend.
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ %{DOCUMENT_ROOT}/$1/$2.php?c=$3 [QSA,L]
RewriteEngine on
RewriteBase /
RewriteRule ^project/category/([0-9a-z]+)$ /project/category?c=$1 [L]
Why is "project/" missing in your original try ?
You have to specify the full path.
You can try this simple rewriteRule wich should works.

htaccess rewrite questions for modifying url structure and redirecting back

I have the following code:
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$
index.php?pageid=$1&pageid=$2&pageid=$3 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?
pageid=$1&pageid=$2 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?pageid=$1 [NC,L]
This makes the following happen:
When someone enters https://www.thegoodmarketer.co.uk/contact-us/ for example it loads the following page https://www.thegoodmarketer.co.uk/index.php?pageid=contact-us
What I want to achieve:
When someone enters https://www.thegoodmarketer.co.uk/index.php?pageid=contact-us I want it to automatically redirect to the correct URL structure: https://www.thegoodmarketer.co.uk/contact-us/
The same applies to all pages.
I've tried so hard to understand htaccess but I cannot get my head around it!
Very grateful for any help please.
Try with below it is for one example of your url, and use end flag on internal redirect rule to prevent recursive redirecting.
RewriteCond %{REQUEST_URI} ^/index.php
RewriteCond %{QUERY_STRING} ^pageid=([^/]+)$
RewriteRule ^ /%1 [QSD,R]
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?pageid=$1 [NC,L,END]

How to remove part of the URL and redirect using mod_rewrite in htaccess?

I would like to remove part of the url of my Joomla site and redirect to another url for example
www.example.com/xxx/yyy/zzz/FAQ
www.example.com/123/456/FAQ
to
www.example.com/FAQ
I'm very new to mod_rewrite in .htacesss
what RewriteRule can I write to achive this
thank you
Try this in root/htaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} /xx/yy/zz/([^\s]+) [NC]
RewriteRule ^ /%1? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /xx/yy/zz/$1 [NC,L]
And the same rule can be used to rewrite /123/456/faq to /123/456/FAq , just change the rewrite target and the pattern in first Rewritecond.
$1 is part of the regex in rewrite rule, its the part matched between (.+) ,it contains uri.
Thank you, after I tried I finally got it working
this is my solution
RewriteEngine On
RewriteRule ^.*/FAQ$ /FAQ [L,R=301]
by doing this I remove everything between the URL and FAQ
www.abc.com/SOMETHING/SOMETHING2/FAQ
and redirect the URL to
www.abc.com/FAQ
I also found a nice tool to test the mod_rewrite code
http://htaccess.madewithlove.be/

Using .htaccess rewrites to treat variables as folder names

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.

htaccess rewrite without extension

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?).

Resources