htaccess redirect trouble - .htaccess

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]

Related

.HTACCESS RewriteRule force default page to HTTP

I did a lot of searching on this, and just couldn't quite find the right answer. A lot of things got me close, but nothing worked like I wanted it to.
I'm working on a site where, because of an external JQuery add-on library, I need to force certain pages to be HTTP. Certain other pages (for shopping, etc) need to be HTTPS.
So far in my .HTACCESS file I have:
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^(index|event)\.php$ http://www.example.com%{REQUEST_URI} [R=301,L]
This works perfectly when the user goes to
https://www.example.com/index.php
But doesn't redirect when they go to
https://www.example.com/
Any suggestions on how to catch that last instance?
Thank you in advance!
To match landing page tweak your regex to match empty URI also like this:
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^((index|event)\.php)?$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC,NE]

General rulefor redirect in htaccess

I want to make some redirects in htaccess. I will explain in detail what I'm trying to do and at the end I will tell you my question, so if you don't want lots of explains just scroll down.
So for you to understand what I am doing, here is an example: if I enter www.mysite.com/oldpage it should redirect me to www.mysite.com/new page. For this I used:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
Redirect /oldpages http://www.mysite.com/newpages
The problem is that I want to use this for more than one page. So I want that:
mysite.com/oldpage/firstarticle
to redirect to
mysite.com/newpage/firstarticle
And the same with all articles. I want to redirect all like:
Redirect /oldpages http://www.mysite.com/newpages
Redirect /oldpages/firstarticle http://www.mysite.com/newpage/firstarticle
Redirect /oldpages/secondarticle http://www.mysite.com/newpage/secondarticle
But I have a conflict because Redirect /oldpage works on every case.
That's why (AND HERE IS WHAT I DON'T HOW TO DO!!!) I need to use a general rule like:
Redirect /oldpage/(*) http://mysite.com/newpage/(*)
Redirect /oldpage/(any numeric characters-don't need them)/(*) http://mysite.com/NEWSINGLEPAGE/(*)
Instead of (*) it should be any characters,but use them in the new link.
Does someone know how to make this Redirect for more than one page?
Thank you for your time!
!!!!
I DON'T KNOW IF IT'S POSSIBLE BUT IT WOULD BE NICE IF I COULD USE AN IF STATEMENT IN HTACCESS...SOMETHING LIKE:
if (adress is /redirect) Redirect to newpage.html;
else if (adress is /redirect/(some number)/(.*)) Redirect to NEWSINGLEPAGE/$1.html
else if (adress is /redirect/(.*)) Redirect to newpage/$1.html
Change
Redirect /oldpage/(*) http://mysite.com/newpage/(*)
To
RedirectMatch /oldpage/(*) http://mysite.com/newpage/$1
try this
RewriteRule ^oldpage/(.*)$ /newpage/$1 [R=301,L]

httacces Rewrite URLs

I wanted to rewrite some urls from
www.example.ro/women_shoes.php to www.example.ro/women-shoes/
and with pagination looks like
www.example.ro/women_shoes.php?page=1 to www.example.ro/women_shoes/pagina-2/
www.example.ro/men_shoes.php to www.example.ro/men-shoes/
with pagination like the first one
www.example.ro/sandale_barbati.php to www.example.ro/sandale-barbati/
and so on..
for the last example i tryed
RewriteRule ^sandale-barbati/pagina-([0-9]+)/ /sandale-barbati.php?page=$1 [L]
RewriteRule ^sandale-barbati/?$ /sandale_barbati.php?$
but i need to write that for every page...
is there a way to do that automaticaly for all pages ?
thanks
If you always use the same naming for all your pages, this will work:
RewriteRule ^([a-zA-Z]+)([-_]){1}([a-zA-Z]+)/pagina-([0-9]+)/$ $1_$3.php?page=$4
RewriteRule ^([a-zA-Z]+)([-_]){1}([a-zA-Z]+)/$ $1_$3.php
RewriteRule ^([a-zA-Z_]+)/pagina-([0-9]+)/$ /$1.php?page=$2
RewriteRule ^([a-zA-Z_]+)/$ /$1.php
You can surf to /women_shoes/ and it will be the /women_shoes.php page.
You can surf to /women-shoes/ and it will be the /women_shoes.php page.
You can surf to the /shoes/ and it will be the /shoes.php page.
I hope this has met all your requirements and was of sufficient help to you. For future reference, the website http://www.regular-expressions.info/ is very handy when dealing with RegEx (such as .htaccess rewrite rules).

.htaccess 301 redirect with query

I know there are tons of these questions out there, but I still don't understand my issue. Thanks to a miscellaneous link found on Google, we've found a bad link for this website. I wrote a rewrite condition for it that is the below:
RewriteCond %{QUERY_STRING} ^t=&s=BARREEL+YOUR+OWN+BOURBON+RISKY+WHISKY+BARREL+PACK+$
RewriteRule ^/results.php$ http://www.thepartysource.com/express/item.php?id=28952 [R=301]
The '+' are in the actual link. I've tried writing it as follows so that it doesn't count them as special characters:
RewriteCond %{QUERY_STRING} ^t=&s=BARREEL\+YOUR\+OWN\+BOURBON\+RISKY\+WHISKY\+BARREL\+PACK\+$
RewriteRule ^/results.php$ http://www.thepartysource.com/express/item.php?id=28952 [R=301]
The site still takes me to the wrong page and does not redirect me. Can someone help with this please? Thanks in advance.
EDIT:
The URL is
/results.php?t=&s=BARREEL+YOUR+OWN+BOURBON+RISKY+WHISKY+BARREL+PACK+
Rather than getting out RewriteRules you can use Redirect instead
Redirect 301 /results.php?t=&s=BARREEL+YOUR+OWN+BOURBON+RISKY+WHISKY+BARREL+PACK+ http://www.thepartysource.com/express/item.php?id=28952 [R=301]
However, it's worth noting, that Google has said you should Disallow/no-index search result pages on your site.

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