Use subdomain to make get parameter for php - php

I found couple of same questions but their answers not working for me. They dont make folders acceptable. Here is my question:
I want to translate:
test.localhost/folder/&anothervar=1
to
localhost/folder/index.php?subdomain=test&anothervar=1
I will use it on some domain too I'm developing the site right now so I need to figure out how to make this work before another parts.
Thanks,

You will have to go into your .htaccess file to solve this.
Once there, enter the following
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.com [NC]
RewriteRule (.*) http://www.%2.com/?test=%1 [R=301,L]
This will essentially convert subdomain.domain.com into domain.com/?test=subdomain
You can take it from there and add additional parameters.
For other subdomain related stuff, check this out.
Hope that helps!

To translate
test.localhost/folder/&anothervar=1
to
localhost/folder/index.php?subdomain=test&anothervar=1
You can use the following rule :
RewriteEngine on
#1)if host value is =="test.example.com"
RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.([^.]+) [NC]
#2)And the uri is =="folder/&anychar"
#Redirect the request to "http://%1/folder/index.php?test=sub&anychar
RewriteRule ^([^/]+)/&(.+)$ /$1/index.php?test=%1&$2 [B,R=301,L]

Related

PHP - How to avoid infinite loop in Redirect by htaccess

I want to redirect my visitors to a specific virtual url according the country they are. For example, if you are in Usa and enter www.mysite.com/subsite, you are redirect to www.mysite.com/subsite/US/
If you enter www.mysite.com/subsite/contact.php you have to be redirect to www.mysite.com/subsite/US/contact.php. Same for another country, for ex, Argentina (AR), you will be redirect to www.mysite.com/subsite/AR/contact.php . In the back, you will always see the physical content of www.mysite.com/subsite/contact.php.
I was reading a lot about this and try to do it by editing htaccess, and it works (sort of...) but I got an infinite loop. I try to make a rule that says "if you don't have the string 'AR' in your url, make the redirect, otherwise, stay as you come", but it isn't working. Could anyone please help me. Here's my code so far:
GeoIPEnable On
RewriteEngine On
RewriteOptions MaxRedirects=1
RewriteCond %{HTTP_HOST} ^(?!.*AR).*$ [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AR$
RewriteRule ^(.*)$ /subfolder/AR/$1 [R]
Your problem is this line:
RewriteCond %{HTTP_HOST} ^(?!.*AR).*$ [NC]
The variable %{HTTP_HOST} is the domain name requested by the browser. To check the path, you would use %{REQUEST_URI}:
RewriteCond %{REQUEST_URI} ^(?!.*/AR(?:$|/)) [NC]

Unable to remove query strings from URL using .htaccess

My URLs look like http://example.com/?n=x. I want the URL to show as http://example.com/. I have used to approaches so far and none of them works.
First one is based on this question:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=1$
RewriteRule (.*) $1? [R=permanent]
After the answer below I modified the .htaccess file:
```RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule (.*) $1? [R=permanent]```
but it still did not work. Here is the debugging info:
RewriteCond %{QUERY_STRING} ^n=(.*)$
This condition was met.
RewriteRule (.*) $1? [R=permanent]
The new url is http://example.com/blog/cat/??
Test are stopped, a redirect will be made with status code permanent
Second approach is:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ $1 [QSD]
None of them seem to work.
I have a few more questions:
Does rewriting only work if the URL is not typed manually?
Can I still access the values of query strings in PHP?
What happens when a user copies the rewritten URL?
The first approach is correct if you go to http://example.com/?n=1, if I am correct you should change ^n=1$ to ^n=(.*)$.
And the other questions:
It works for all kind. It doesn't matter if it was a robot or a human writing it, when you access a page, .htaccess will be read.
Yes you can. Use $_SERVER["QUERY_STRING"]. If you use the htaccess redirection before explained, you will lose them because you are redirecting.
What do you mean ?

htaccess rewrite URL if domain doesn't equal and subfolder is this

I'm trying to get a rewrite working and need a hand...
I've got a set of websites, which domains are like this
example.com
example.co.uk
example.ie
example.com/gm
The way the site is setup means that all domains can display the .com/gm content (GM doesn't have it's own domain and we want it only served from the .com domain)
So I'm trying to redirect anyone using incorrect URLs to the correct TLD with the gm subfolder. E.G:
going to www.example.co.uk/gm would 301 to www.example.com/gm
I've currently got this:
RewriteCond %!{HTTP_HOST}:{REQUEST_URI} !^example.com:gm$ [L,R]
RewriteRule ^(.*)$ https://www.example.com/gm$1 [R=301,L]
Which obviously isn't happy
Appreciate a point in the right direction, basically from what I can tell I need a rule that checks the domain doesn't equal .com if they're trying to get to /gm/ and redirects them to .com/gm/
Thanks!!
It is easier to check if the /gm/ path was requested in the RewriteRule, and prefix that with a RewriteCond that checks for the host name – something like this should do it:
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(gm/.*)$ https://www.example.com/$1 [R=301,L]
Your htaccess is full of syntax errors.
Try the following:
RewriteCond %{HTTP_HOST} ^(www\.)?example.co.uk$
RewriteCond %{REQUEST_URI} ^/gm/
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
This will redirect
example.co.uk/gm
to
example.com/gm
Clear your browser's cache before testing this.

multiple rewrite for a single url using htaccess

I have a multi blog website in which there are multiple domains.
The thing is i need a rewrite rule for the following syntax
XXX.domain.com/{any other things} to www.domain.com/domain/XXX/{any other things}
Also i had written a set of rules for the below one too
^domain/([a-zA-Z0-9]+)/cat/([a-zA-Z0-9]+)$ index.php?domain=$1&cat=$2
So the things is i need to construct a rule which converts domain to path then again process the other things as follows with that.
Before your other rule, you'll need
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond $1 !^domain/
RewriteRule ^(.*)$ /domain/%1/$1 [L]
RewriteRule ^([a-zA-Z0-9\-_]+).domain.com/(.*)$ www.domain.com/$1/$2
([a-zA-Z0-9\-_]+) matches a subdomain.
(.*) matches everything.
This will rewrite whatever.domain.com/anything to www.domain.com/whatever/anything.
I hope I understood correctly, that your redirect example has nothing to do with the requirement:
RewriteRule ^([^.]+)\.domain\.com/(.*)$ www.domain.com/domain/$1/$2
[^.]+ captures the subdomain, the rest is selfevident, I hope.

I need a clean url without the parameters

Hello i need to make my url clean and i just do not know were to start as it is mind boggling, i have read numerous things in regards to clean urls but i have no idea.
This what i am getting on woorank as i am doing my seo.
Warning! We've detected parameters in a significant number of URLs.
I am unsure if this is right i have taken my real domain out and put my site instead
#RewriteCond %{HTTP_REFERER} !^$
#RewriteCond %{HTTP_REFERER} !^//(\.)?My site/.*$[NC]
#RewriteRule .(png|gif|jpg)$ – [F]
RewriteCond %{HTTP_HOST} !^ My site.co.uk$ [NC]
RewriteRule (.*) My site.co.uk/$1 [L,R=301]
Thank you J C
A basic htaccess can look like this:
RewriteEngine on
RewriteBase /
RewriteCond %{http_host} ^mysite.co.uk [nc]
RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [r=301,nc]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/\.]+)/?$ index.php?p1=$1 [L]
For every new parameters you just add ([^/.]+)/ and extra parameter in the end..
What happens in code is up to you, but you will need a standard way of working if you want to use something like this.. .
NOTE: you can't just implement this now, because your site needs to be fully build to the structure of your htaccess.. So if you would replace this now, alot of others things might get broken soon... .
It depends. As Naruto pointed out. We need to know the structure of your code. Give some examples of how the urls are now and what you want them to look like. The examples will explain a bit how you might have programmed the website.
e.g. different php file for each page /about.php, /register.php or a single entry /index.php with every page having the same parameter keys.
/index.php?page=page1&foo=bar&qux=norf
/index.php?page=page2&foo=bar
or perhaps each page has different parameter names
/index.php?page=page1&foo=bar
/index.php?page=page2&qux=norf
What you can always do is redirect to a single index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
index.php now needs to route everything approperiately. This basically does the same as what Naruto suggests but instead in php directly and this might be easier for you.
How this routing happens depends on your code and is up to you. But let us assume that you have a different file for each page with different parameters. You could do this without changing the rest of your code.
// FROM: /shop.php?category=software&subcategory=webdevelopment
// TO: /shop/software/webdevelopment
$path = explode('/',$_SERVER['REQUEST_URI']);
if($path[0] == 'shop') {
$_GET['category'] == $path[1];
$_GET['subcategory'] == $path[2];
include('shop.php');
}
This way only one file needs to be edited and the rest of your code can still work with the $_GET. This is the same as what you would do in your htaccess.

Categories