PHP / htaccess redirect via GET - php

I want:
sub.domain.com to load index.php
sub.domain.com?s=custom to load /custom/index.php
I'm doing this right now:
In .htaccess:
RewriteRule ^(custom)/?$ index.php?subject=$1
and in index.php I have this:
if($_GET['s'] == 'custom') {
header( 'Location: http://sub.domain.com/custom/index.php' ) ;
}
... but is it possible to do the redirect via htaccess itself depending on the GET variable?
Thanks!

Yes, you could check query string with RewriteCond
RewriteCond %{QUERY_STRING} s=custom
RewriteRule ^(.*?)$ custom/index.php [L]
With this, it redirects all requests with existing "custom" get parameter to index.php, and this can be extended :)
Idea: http://statichtml.com/2010/mod-rewrite-baseon-on-query-string.html

You probably want to use "RedirectMatch"
RedirectMatch ^sub.domain.com?s=custom$ /custom/index.php
Maybe youll need to play with the regex abit

Related

Is there a function or way to do in PHP similar how this works in Node JS?

In Node we can get an url address with a structure like this /example/:page/:id and we can take the page and id params. Is there a possibility to do something similar using PHP? Or is it only possible using the "?" with all the wanted params after the interrogation point?
I searched for a while and I tried some configurations in the htaccess file. All of them gave some kind of error like 403, 404 or in one of the configurations the intentioned page was loaded but it didn't find the css, js and images files.
Thanks
Edit:
I will put the solution I found here because maybe it can be useful for someone someday. After looking for some routers packages, I saw them instructing to put these lines in the htaccess file:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
I've tried something like this before and it was the one that I mentioned in the question that loaded the page but it didn't find the files like css, js, etc.
So I've decide to check the base url and I saw this was the point where the error was coming. After I changed it, the page loaded as the expected and now it's possible to get the value where the users can put a number and redirect to the page that they want (it's something like a magazine).
You can achieve it many ways.
In Laravel (see Documentation). I think every framework now has routing implemented.
Route::get('example/{page}/{id}', function ($page, $id) {
//
})->where(['page' => '[0-9]+', 'id' => '[a-z]+']);
With Mod-rewrite and then with access through $_GET parameters.
RewriteEngine on
RewriteRule ^example/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?page=$1&id=$2 [NC]
You can also redirect everything to index.php and there implement your own router. See: Redirect all to index.php using htaccess
RewriteEngine on
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
Something like this could work
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = explode( '/', $uri );
// all of our endpoints start with /person
// everything else results in a 404 Not Found
if ($uri[1] !== 'page') {
header("HTTP/1.1 404 Not Found");
exit();
}
For more reference visit this url
https://developer.okta.com/blog/2019/03/08/simple-rest-api-php
have you tried parse_url()?
it will return and associative array which has all the components in your URL

hide incoming querystring with htaccess

I want that anybody who comes to my site
site.com?affiliate=a12b345c67
hides the affiliate querystring completely
I'm sure it's something like this, but nothing happens
RewriteRule ^/?affiliate= / [L,R=301]
To strip affiliate= query string from your URLs, place this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^affiliate=[^&]+ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301,NE]
First of all you need to charge the URL from which users are coming to your website. Change it to:
site.com?/a12b345c67/
.htaccess cant change your URL structure on the fly.
Let me know if its ok.
Regards.
you could save the incoming $_GET parameter(if exists) to a session(or cookie, or whatever persistent storage) and reload the page.
session_start();
if(isset($_GET['affiliate']){
$_SESSION['affiliate'] = $_GET['affiliate'];
header('location: site.com');
}
you could then use the affiliate token from the session,cookie or whatever persistent storage

htaccess rewrite url rule trouble detect $_GET isset

RewriteEngine On
RewriteRule ^about/([^/.]+)/?$ about.php?id=$1 [L,QSA]
I have a page use htaccess rewrite rule for url
/about/33
the page require $_GET['id'] to fetch db
however i have trouble to detect GET variable isset
if(isset($_GET['id'])){fetch data...}else{header("location:...");}
if user only enter /about/ without $GET['id'] the page will not found instead run header("location");
is anyway to solve this?
try this:
RewriteRule ^about/([^/.]*)/?$ about.php?id=$1 [L,QSA]
Put * instead of +

.htaccess writerule to remove question marks and equal mark

today i tried to create .htaccess file that replacing ? and = with / ,
test.php code:
<?php
echo $_GET['myparam'];
?>
.htaccess:
i used this writerule:
RewriteEngine On
RewriteBase /
RewriteRule ^test/myparam/([0-9]+)/?$ test.php?myparam=$1 [NC,QSA,L]
ok, i navigated to www.web.com/test.php?myparam=123
there is no redirect to www.web.com/test/myparam/123
so navigated to: www.web.com/test/myparam/123 (Manually) and the php script is worked,
i changed the myparam value to abc instead of 123 : www.web.com/test/myparam/abc
and then it redirects to 404 not found page...(the server don't know that abc is not directory when integer works when string 404)
!so what i want to do:!
www.web.com/ test .php? inttParam = 1 & strrParam = stringhere & p = 1
TO
www.web.com/ test/inttParam/1/strrParam/stringhere/p/1
and when i use $_GET['p'] it will work.
i changed the myparam value to abc instead of 123 and it didn't work
Well of course it won't work since your rule is matching only numbers in the end:
RewriteRule ^test/myparam/([0-9]+)/?$ test.php?myparam=$1 [NC,QSA,L]
change your rule to this to make it work with anything:
RewriteRule ^test/myparam/([^/]+)/?$ test.php?myparam=$1 [NC,QSA,L]
To make it recursion based generic rule to convert /test/inttParam/1/strrParam/stringhere/p/2 to /test.php?p=2&strrParam=stringhere&inttParam=1`:
RewriteRule "^(test)(?:\.php)?/([^/]+)/([^/]*)(/.*)?$" /$1.php$4?$2=$3 [NC,L,QSA]
RewriteEngine on
# do not affect files
RewriteCond %{REQUEST_URI} !(\..{2,4})$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ index.php?parameter1=$1&%1 [L]
Something like that perhaps?
ok, i navigated to www.web.com/test.php?myparam=123
there is no redirect to www.web.com/test/myparam/123
I don't know if that means that you also want to make a redirection (presumably 301) from the one with GET params to the one with slashes.
If so, I would recommend to use the previous answer from anubhava and handle any 301 redirection with PHP. Actually, you can redirect with "RewriteRule" using the "R" flag, but I admit that I woundn't know how to solve this particular case.
Anyway, I think there is no need, unless old URLs with GET params were working well at SEO and you didn't wan't to lose ranking.

How create settings but global redirect?

I need to, when a person comes to mysite.com/index.html -> refirect to mysite.com/index.php or
mysite.com/index.phtml -> mysite.com/index.php. or
mysite.com/index.sdsf -> mysite.com/index.php. or
mysite.com/about.phtml -> mysite.com/about.php.
If you want to catch incorrect filenames, then that's something you have to do outside of PHP. Try a mod_rewrite rule like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index\.(?!php)\w+$ index.php [L,R]
You want all incoming requests to direct to /index.php using PHP?
You could try...
if ( ! preg_match('/\/^index\.php/', $_SERVER['REQUEST_URI'])) {
location('Header: /index.php');
exit;
}
Note, you should use the full URL in the redirect.
Otherwise, if you wanted to select those ones you mention exclusively, you could build an array of them, and then loop and check.
Why do you need to redirect it? Why not just use index.php as your default page?
Anyway, a good way to do this would be by use of header
header('Location: http://mysite.com/index.php');
Hope that helps.

Categories