Redirecting url with cdn in htaccess file - php

from
cdn1.example.com
cdn2.example.com
cdn3.example.com
to
example.com
Please let me know how to redirect those?....

You could have provided more details and also what you have tried, anyways here you go:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(cdn1|cdn2|cdn3)\.example\.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
As a bonus, any eventual path and/or querystring is preserved, I guess you would need that.
EDIT : As you requested, here is a cdnx version:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^cdn[0-9]+\.example\.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Related

HTTP to HTTPS redirect issue .htaccess

I'm a newbie to coding and I'm using following codes in my .htaccess.
My issue is my site is not redirecting from http://example.com to https://example.com with this code. Did research but couldn't find something that goes with my code.
I do not know if this is the correct order, or the most efficient way to achieve this. Any input would be greatly appreciated!
Remove WWW, from both HTTP and HTTPS an force HTTPS
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ https://%3%{REQUEST_URI} [R=301,L]
Hide .php, and redirect .php files to new non-extension URLS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301]
Give this a go and let us know if it works out for you. looking at your .htaccess for your SSL looks like it has way more in it then there needs to be. This is what I use and seems to work fine
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This will redirect all traffic to an https://

Conditional htaccess

I just want some help with my .htaccess configuration. Before I have this problem where I need to redirect the page to
a specific url. Like if the user type http://mydomain/test it will be redirected to http://mydomain/app/r/test. I
achieved the redirection using .htaccess. The code is below
RewriteEngine on
RewriteRule (.*) http://mydomain/app/r/$1 [R=301,L]
But I have this new specification. As follows:
If user visit http://mydomain/ or http://domain/index.php, he will be
redirected to http://mydomain/mymanager
If user visit
http://mydomain/{any_segment}, he will be redirected to
http://mydomain/app/r/{any_segment}
Here is my current htaccess. Basically in the url, any characters will be redirected to /app/r/{} except index.php. But what about the http://domain/? How can I achieve this.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain$
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L]
I've been playing around my htaccess and any pointers will be of great help to me.
These are your rules :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain$
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L]
It looks like , any request should redirect to /app/r/ except A URI start with /index.php but still not doing that , in normal cases , so you should exclude the target directory from being redirect again and again by this rule RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L] , ^(.*)$ means any URI start with any thing and the server will look, if there is a condition , and actually there is only one condition to exclude index.php, otherwise redirect .
So , you should exclude this target directory along with index.php like this :
RewriteCond %{REQUEST_URI} !^(/index\.php|/app/r/)
But still you want to match a request that target HOST only so,you should add that as well :
RewriteCond %{REQUEST_URI} !^(/index\.php|/app/r/|/?$)
Then you could match host with www or non-www like this :
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain$
Your rules should look like this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain$
RewriteCond %{REQUEST_URI} !^(/index\.php|/app/r/|/?$)
RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L]
Also you could summarize that more by utilizing %{REQUEST_URI} like this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain$
RewriteRule !^(index\.php|app/r/|/?$) http://mydomain/app/r%{REQUEST_URI} [R=301,L]
NOTE: Clear browser cache then test

replacing Login URL to short URL with .htaccess

I have an URL as :
http://mydomain.com/site/?cmd=home
I want to change the above address to http://mydomain.com/home
i am using .htaccess file like this:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /site?cmd=$1 [L]
I am not sure where is the problem?
Thanks in advance
The Rule works the other way around: you want to get from /site/?cmd=home to /home, but with your RewriteRule you are redirecting to /site?cmd= (Probably there is a mistake somewhere?)
If you want to redirect from /site/?cmd=home to /home, like the question states, use the following:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/site
RewriteCond %{QUERY_STRING} ^cmd=(.*)$
RewriteRule ^(.*)$ /%1/? [R=302,L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /site\?cmd=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^([^/.]+)/?$ /site?cmd=$1 [L]

htaccess URL rewrite - requested URL not found on server

Having an issue where the URL rewrite is running but the server will not display the contents. My .htaccess file is
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project\.php [NC]
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule . project-%1.html? [R=301]
Thanks for your help!
EDIT: Sorry, here is some more info!
I want to rewrite
www.siteurl.co.uk/project.php?id=82
to
www.siteurl.co.uk/project-82.html
The code in the .htaccess rewrites the URL perfectly but the page does not display. I get a
404 The requested URL /project-82.html was not found on this server.
I hope that helps!
You only have one half of the solution, creating the pretty urls. What is missing is the other half, sending the pretty urls to an application for processing as below
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project\.php [NC]
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule . project-%1.html? [R=301]
#you need this rule to process your www.siteurl.co.uk/project-82.html request
RewriteRule ^project-([0-9]+)\.html$ project.php?id=$1 [L,NC,QSA]
try putting RewriteRule ^project-%1.html? [R=301] instead of the .
Don't know what the "[A-Z]{3-9}\ " is for? maybe I'm missing something?
But maybe something like this instead:
RewriteCond %{THE_REQUEST} ^project\.php [NC]
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule . project-%1.html? [R=301]
or
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^/project\.php project-%1.html? [R=301]
Note that this is just off the top of my head, and I didn't test it.
EDIT: Just adjusted the second example which had an extra slash in it, and tested it out on my webserver (note that I'm using IIS/URLRewrite, but should be same):
RewriteBase /
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^project\.php project-%1.html? [R=301]
I entered http://localhost/project.php?id=123 and it went to http://localhost/project-123.html
But I think I missed something... you say the url rewrite is working, and the url is changing but you're getting a 404? In that case then your rewrite sounds fine - and something else is not permitting your html files to be accessed (or they don't exist)?

htaccess redirect help please

I previously asked this question and the answer did not seem to work as required - it also required an addition.
Users are provided with unique URL's, ie: exampleurl.com/AQ4ILB9
AQ4ILB9 being the referral code
I would like that URL above (or any referral URL) to display the contents of index.php (htaccess redirect?)
I would like the non-www and www version of exampleurl.com, including example.com/index.php to be redirected to the top level in this format: http://www.exampleurl.com/
I would like for example: $_GET['_url'] to hold the referral id (ie: AQ4ILB9) in index.php
How can I go about the above 3 all using htaccess?
Thanks!
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)$ /index.php?_url=$1 [NC,L,QSA]
The first part is a 301 redirect to add www.
The second part will rewrite what you want, but not for existing files/directories.
Q) 1 & 3:
RewriteRule (.*) index.php?url=$1 [L,P]
Q) 2:
RewriteCond %{HTTP_HOST} ^exampleurl.com
RewriteRule (.*) http://www.exampleurl.com/$1 [R=301,L]
RewriteRule ^/([A-Z0-9]+)$ index.php?_url=$1
Replace example.com with your domain and put this in your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule (.*) /index.php?_url=$1 [L]
Should do the trick! You may not need the RewriteEngine On line if it's already there.

Categories