detect and redirect based on URL - web-services

I have the following conditions in my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ex\.co$
RewriteRule ^(.*)$ http://example.com/?ref=$1 [L,R]
This will forward ex.co\xxxxxx to example.com\?ref=xxxxxx.
What I want is to add a line that if a user simply goes to ex.co it redirects to example.com and not (as it does currently) example.com?ref=
Any suggestions?
Thanks!

I think you just need to add the third line below:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ex\.co$
RewriteRule ^$ http://example.com [L,R]
RewriteRule ^(.*)$ http://example.com/?ref=$1 [L,R]

Related

Why does my htaccess not rewrite properly

I am having a problem with my htaccess file, and can't figure out if it's my configuration, or something like server cache which is messing with my URLs.
My file is the following
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^website.com$
RewriteCond %{HTTPS} off
RewriteRule (.*) https://website.com/$1 [R=301,L]
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
The first rule is to move www to non-www, it works.
The second rule is to move http to https, it also works.
the third rule is to make URL /anyFile call anyFile.php, but keep a lovely URL.
The problem is that it calls anyFile.html, not PHP, and if I remove said HTML file then I get 404.
Yes it works typing anyFile.php in the URL, but I would like to not have .php in the URL.
If it is not obvious enough, it is supposed to work for any file name, not just a single one.
Any and all help is much appreciated.
Replace your .htaccess rules with this code:
Options -MultiViews
RewriteEngine On
RewriteBase /
# single rule for http->https and www removal
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://website.com/$1 [R=301,L]
# hide .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-z]+)/?$ $1.php [NC,L]
I'm recently learn how to do htaccess too, so I'm still a novice but I would write
RewriteRule ^([a-z]+)\/?$ helloWorld.php [NC]
to redirect it to the helloWorld.php file.
The $1 is a get parameter.

Redirect 301 with params

I am redirecting traffic between old web and new web and I need to redirect 301 from /hotel.php?hotel=lasvegas&lng=es to http://www.new-web.com/hotel-las-vegas/
How could be possible from htaccess?
I have tried
Rewriterule ^hotel\.php\?hotel=lasvegas\&lng=es http://www.new-web.com/hotel-las-vegas/ [L,R=301]
I think my error is at regular expression. Can anyone help me?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /hotel\.php\?hotel=lasvegas&lng=es [NC]
RewriteRule ^ hotel-las-begas? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^hotel-las-vegas/?$ hotel.php?hotel=lasvegas&lng=es [L,QSA,NC]
it's very important that you insert best code of redirect 301 in best place of htaccess. for example :
http://www.abartazeha.com/
we replace abartazeha.com instead of abartazeha.ir
...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]

New .htaccess puzzle

I have this link:
index.php/forums/viewforum/5/
Now, I want that "forums" word in URL to become dynamic such that which ever word I replace it with, it still redirects to the same URL.
For example, if I have:
ProductA/viewforum/5/
it redirects to:
forums/viewforum/5/
For example, if I have:
ProductB/viewforum/13/
it redirects to:
forums/viewforum/13/
In other words, if there's a "view forum" word in the URL, it should trigger this rewrite.
I already have a .htaccess that removes the index.php from the URL so the rewrite rule should consider that too.
Is it possible?
HTACCESS:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteEngine on
RewriteCond %{REQUEST_URI} (member)
RewriteRule ^(.*)$ http://%{SERVER_NAME}/404 [R=301,L]
RewriteRule ^[.*]+/(viewforum/[0-9]+/?)$ /forums/$1 [L,NC]
RewriteCond $1 !^(images|favicon\.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.*)$ /home/ddco/public_html/index.php/$1? [L]
</IfModule>
You can use this rewrite rule:
RewriteCond %{THE_REQUEST} /forums/ [NC]
RewriteRule ^ - [R=404,L]
RewriteRule ^[^/]+/(viewforum/[0-9]+/?)$ /forums/$1 [L,NC]
Also suggesting you to post your current .htaccess rules as well for further investigation.

.htaccess how to redirect subdomain/folder to subdomain only

I'm trying to find an answer on different places, but I can't find the full solution for this.
I had a folder setup for a blog. Now we move to an subdomain setup. The redirect shown below works perfectly. The only thing that doesn't work is when the url is a subdomain combined with folder. that isn't redirected.
RewriteCond %{HTTP_HOST} ^(www\.)domain\.com$
RewriteRule ^blog/(.*)$ http://blog.domain.com/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$
RewriteCond %{REQUEST_URI} !blog/
RewriteRule ^(.*)$ blog/$1 [L,QSA]
www.domain.com/blog/some-url-here is redirected to
blog.domain.com/some-url-here
When I try blog.domain.com/blog/some-url-here it returns a http-status of 200 and when I try to redirect I get a infinitive loop.
Is there a redirect that I've missed?
Try these rules on top of your other rules:
RewriteCond %{THE_REQUEST} \s/blog/([^\s]*) [NC]
RewriteRule ^ http://blog.domain.com/%1 [L,R=301]
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$
RewriteRule !^blog/ blog%{REQUEST_URI} [L,NC]

301 redirect with subdomain

I have put the following content in my .htaccess file. What I want is redirect all mydomain.com/ to www.mydomain.com/. At the same time I want subdomain.mydomain.com/ should not get redirected anywhere. With the below code, I am able to achieve my 1st goal. But it redirects http://subdomain.mydomain.com/ to http://www.mydomain.com/subdomain. Please guide me on how to achieve both my modifying .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mydomain.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
Make it more restrictive, i.e. apply rule only when host name is mydomain.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]