Url Rewriting like Subdomain in PHP - php

I want to rewrite url as like subdomain style.
For example if main url is mysite.com/test than i want to rewrite it like test.mysite.com
mysite.com/test => test.mysite.com
Can you please suggest me what rewrite rule should i need to add in .htaccess file.
Thanks in advance.

RewriteCond %{HTTP_HOST} ^mysite.com$ [NC]
RewriteRule ^(.+)$ http://$1.mysite.com [L,R=301]
Should do it, might need a little tweaking

Related

How to URL rewrite with my case

I know that this site has many questions about URL rewriting. But as a newbie, I could not find the correct answer for my case.
My question is: I would like to rewrite the original URL like this: when user visit:
http://username.example.com/a/b/c/d/e/f/g/h...
the apache will process request as
http://example.com/index.php?user=username&a=b&c=d&e=f&g=h...
According to what I knew, the solution could be using QUERY STRING, but I don't know how to do that.
Any help will be appreciated! Thanks,
The first thing you need for this is to create Dynamic sub-domain. create dynamic subdomain link will help you in this. Use following config for your case:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteRule ^([aA-zZ])$ index.php?user=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com
RewriteRule (.*) index.php?user=%1
once this is in place, you can add more conditions for the remaining parameters.

htaccess rewrite url using last parameter

I have a query on htaccess i need to rewrite a url like below example
www.example.com/parameater1/parameater2/parameater3/name=xyz
i need something like below
www.xyz.example.com/parameater1/parameater2/parameater3
i tried the below code
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.co\.uk$
RewriteRule ^(.*)$ "http://www.example.co.uk/test/$1/$2/$3?user=%1" [P]
but unable to resolve using the above code
Not best, but should work.
RewriteRule ^([^/]*/[^/]*/[^/]*)/name=(.*)$ http://www.$2.example.com/$1 [R=301,L]

Issue with Rewrite Rule in .htaccess file

I was about to convert my webpage url into seo friendly. My url link is like follows
http://example.com/display.php?id=***
Now i want to convert this into seo friendly url, which could be
http://example.com/partners-id-***.html
Something like above. I used
RewriteEngine On
RewriteRule ^partners-id-([^-]*)\.html$ /display.php?id=$1 [L]
Rewrite rule in htaccess file. But still the url stays same as older one. Anyone can help me to fix this issue???
You need 1 more 301 rule before this rule for external redirection:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+display\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /partners-id-%1.html? [R=301,L]
RewriteRule ^partners-id-([^.]*)\.html$ /display.php?id=$1 [L,QSA]
As commented by Justin and other folks it is better to change your links to /partners-id-123.html. However for links already cached by search engines will be taken care by first rule here which will 301 redirect:
/display.php?id=1234 => /partners-id-1234.html
Please try like below:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /display.php?id=$1 [L]

How to make clean URLs display dirty URLs with .htaccess?

I have a slight problem.
I have a site that uses these formats for viewing certain PHP documents:
domainname.com/server.php?id=14
domainname.com/changeserver.php?id=14
domainname.com/customize.php?id=14
I would like to make these:
domainname.com/server/14
domainname.com/changeserver/14
domainname.com/customize/14
I also have various URLs such as /index.php and /login.php I would like to clean up.
If someone were to type the original URL, such as /server.php?id=14, I would like it to redirect to it's clean URL.
If anyone can provide me with a config or help me along the way to making one that can do this, it would be greatly appreciated.
To prevent an infinite loop while externally redirecting the ugly url to the clean url and internally rewriting it back to the ugly url, you can use the THE_REQUEST trick.
#External redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(server|changeserver|customize)\.php\?id=([^&]+)\ HTTP
RewriteRule ^ /%2/%3? [R,L]
#Change above to [R=301,L] once all rules work as expected
#Internal rewrite
RewriteRule ^(server|changeserver|customize)/(.*)$ /$1?id=$2 [L]
You need to rewrite your URLs in .htaccess file.
RewriteEngine On
RewriteRule ^server/(.*)$ server.php?id=$1 [R=301,L]
here is a good guide on URL rewriting
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
In this case you have to use a 301 Redirect of URLS. Here is an example:
Redirect
www.example.com/a1/articles.php?id=1&name=abc
to
www.example.com/article/1/abc
For this you have to modify the .htaccess file with the following:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)&name=(.*)$
RewriteRule ^articles\.php$ /article/%1/%2? [R=301]
RewriteRule ^article/([^-]+)/([^-]+)$ /articles.php?article_id=$1&article_name=$2 [L]
RewriteEngine On
RewriteRule ^server/([A-Za-z0-9-_,()'+]+)?$ server.php?id=$1
RewriteRule ^changeserver/([A-Za-z0-9-_,()'+]+)?$ changeserver.php?id=$1
RewriteRule ^customize/([A-Za-z0-9-_,()'+]+)?$ customize.php?id=$1
To adapt to your code. Here is the answer.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^server\.php$ /server/%1? [R=301]
RewriteRule ^server/([^-]+)$ /server.php?id=$1 [L]
Check the Above code. I think this will help you.

Redirection 301 with htaccess with url variables

I've searched answers on the forum but didn't worked for me.
I have rebuild an website using Wordpress and want to keep old urls.
I have old URLs like : index.php?option=com_content&view=article&id=51&Itemid=67&lang=fr
that i want to move permanantly to url like : /produits-et-services/conseil-et-accompagnement/
Redirect 301 didn't worked because of vars in URL.. and i've try this :
RewriteCond %{QUERY_STRING} ^option=com_content&view=article&id=51&Itemid=67&lang=fr$ [NC]
RewriteRule ^index.php$ http://solunea.fr/produits-et-services/conseil-et-accompagnement/ [L,R=301]
On my wordpress .htaccess but didn't worked either.
Can someone help me please ??
Make sure this rule:
RewriteCond %{QUERY_STRING} ^option=com_content&view=article&id=51&Itemid=67&lang=fr$ [NC]
RewriteRule ^(index\.php)?$ /produits-et-services/conseil-et-accompagnement/ [L,R=301,NC]
is placed right on top before any other existing rewrite rule
and test in a different browser to avoid 301 caching issues.

Categories