How to use .htaccess redirect - .htaccess

Okay, so I here .htaccess redirect is a pretty big deal, but I dont know how to do it. Honestly I'm a n00b, so can someone tell me how this is even accomplished? So far I know you have to have a page called
example.com/example.html
and then you want it to look like this:
example.com/example/
My problem is, how do you do that?

That can be achieved by adding the following to your htaccess
RewriteEngine On
RewriteRule ^(.*).html$ /$1/ [R,L]
The above will change any page with a html extension to FILEANME/

Related

htaccess is working but does not replace the url

I'm trying to modify the subdomain name in the URL to make it look nicer. My current URL look something like:
www.mystore.com/productInfo.php?cPath=11_11&productID=222
So, I want to make it nicer by Rewrite in .htaccess in main with this code:
RewriteEngine On
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]
When I run and test it on the URL by typing www.mystore.com/productInfo/11_11/222 in the URL it works well. However, when this page is redirected by a different page or is 'refreshed' with a self redirecting a href= link(in which the link is written in php by the previous programmer), the above old link is still visible in the URL instead of the new one.
I am still a beginner while I suspect that I might need to change something in the cPanel/Apache(I think) for this but currently, I am still do not have access to the cPanel control. Is there anything that I might have missed to write in the .htaccess file or I really do need the cPanel control or any other reasons?
Any help is appreciated. Sorry that I could not find similar questions on this.
You can use the following :
RewriteEngine On
RewriteBase /
#redirect /productInfo.php?cPath=foo&productID=bar to /productInfo/foo/bar
RewriteCond %{THE_REQUEST} /productInfo\.php\?cPath=([0-9_-]+)&productID=([0-9_-]) [NC]
RewriteRule ^ productInfo/%1/%2? [L,R=301]
#rewrite new URL to the old one
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]

URL Rewriting using mod_rewrite

i'm trying lear to make a simple url rewrite and can't understand why is not working.
I created the file .htaccess with this:
RewriteEngine On
RewriteRule ^section\.php$ /how-does-it-work/ [R]
All i want to do is /section.php be displayed as /how-does-it-work/
i'm following a tutorial but doesn't seem to work.
Nothing seems to work… i even tried a mod_rewrite generator, i'm using GoDaddy, is there anything i should activate or something? 'cause so far i'm only dealing with the .htaccess file.
Thanks
You are using them the wrong way around. Try:
RewriteRule ^how-does-it-work/?$ /section.php [L]

htaccess redirect trouble

I use mod-rewrite and I do this:
RewriteRule ^brand/([A-Za-z0-9-]+)/([A-Za-z0-9-\.\_]+)/?$ model.php?ref=$2&brand=$1 [NC]
and it works great! But I have a problem: when you go by url like
brand/stuff/ffgfgfgfg.php it will redirect to a page brand/stuff/ffgfgfgfg?ref=ffgfgfgfg.php&brand=stuff. I want to get rid of such a situation as it is not very good for the promotion on Google.
Maybe anyone can help me out and suggest a solution to redirect that awkward page?
Well here are your options. If, as you say in your comment (although I find it a bit strange) you want to redirect brand/stuff/ffgfgfgfg.php to brand/stuff/ffgfgfgfg, you can use
RewriteRule ^brand/([A-Za-z0-9-]+)/([A-Za-z0-9-\.\_]+).php/?$ brand/$1/$2 [NC]
Apparently, if you want to redirect to brand/stuff/ffgfgfgfg?ref=ffgfgfgfg&brand=stuff, you could use
RewriteRule ^brand/([A-Za-z0-9-]+)/([A-Za-z0-9-\.\_]+).php/?$ model.php?ref=$2&brand=$1 [NC]

.htaccess freindly url help on Rewrite

If I want to use .htaccess to rewriting this:
http://example.com/article.php?article_id=79
to this:
http://example.com/article/my-cool-and-special-article-79.html
what is the rewriting I need to do?
The problem is going to be that Apache will not be able to determine what friendly URL to use from an ID. I think what you want to do is to do the rewrite the other way. What CMS are you using?
Not a machine where I can test, so you may need to dink with it. But, it would look something like this:
RewriteCond $1 ^article [NC]
RewriteRule ^([a-zA-Z0-9\-]+)\-article\-([0-9]+)\.php$ /article.php?id=$2 [L]

Redirect dynamic subdomain to same subdomain with subpage. How?

I'm a little stuck in here. I need to get some help with this subdomain-situation.
I need to redirect http://dynamicsubdomain.example.com/ to
http://dynamicsubdomain.example.com/account/welcome.html.
How do I do this? I tried several things but all without result. The main problem is that I can't fetch the entered dynamic subdomain from the %{HTTP_POST} string from mod_rewrite.
Another issue would be that it's creating and endless loop. So it only needs to redirect on these conditions, not when there's a URL like http://dynamicsubdomain.example.com/test/page.html. Because else it will create and endless loop. It's just for the starting page from the website.
I hope y'all can help me out, it's one of the last but important things from my project.
Thanks in advance!
There are several options on the URL redirection wiki page. For example, how about dropping an index.php in the root that redirects to the destination?
header("Location: http://dynamicsubdomain.example.com/account/welcome.html");
Why does the domain matter so much if you are staying on the same domain, and just redirecting to a different path?
The UseCanonical setting in Apache may have an effect on this, but it is defaulted to on, which preserves the host and port specified in the request.
RewriteRule ^/$ /account/welcome.html [R,L]
Hey guys, thanks for your support/help but I found the solution myself. Quicker than I thought :)
This is what does the trick, I hope I can help someone with this:
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{HTTP_HOST} ^([A-Za-z0-9]+).example\.com [NC]
RewriteRule ^ http://%1.example.com/account/welcome.html [L]
#gahooa: I need to do this because my mainpage example.com is just a sort of landing-page with no special things. The extra part of the URL "account/welcome.html" is for showing a page related to the "subdomains"-account (gahooa.example.com for example will show your profile page). In my app I catch up the subdomain by a preg_match so it knows witch account it has to load. I hope I'm clear :) But thanks anyway!
This is my first time i'm using Stackoverflow but it actually works great! Quick replies from experts, great work! I definitely will come back :)
If you really want to use HTTP_HOST:
RewriteRule ^$ http://%{HTTP_HOST}/account/welcome.html [L,R]
But like gahooa already said you don’t specify an absolute URL if you stay with the same host. The URL path will suffice:
RewriteRule ^$ /account/welcome.html [L,R]

Resources