After adding "Options -MultiViews" URL Rewrite - $_GET variable not passing through - php

I have a simple rewrite that changes
http://blog.website.com/post.php?linkcheck=dkdkdk
into
http://blog.website.com/post/2s33dd
using the following rewrite
Options -MultiViews
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^post/(.*)$ ./post.php?linkcheck=$1 [QSA,NC,L]
The rewrite works, it displays the page (i don't get a 404), but it doesn't appear to be passing through the GET['action'] from the URL.
Trying to remove cookie and window.href.replace it nothing happens, cookie still present.
I saw a post that suggested i put Options -MultiViews still does not respond too the GET parameter
<?php
if (isset($_GET['action'])){
if ($_GET['action'] == "logout"){ ?>
<?php
setcookie("blog_id", "", time() - 3600); //cookie not being removed
?>
<script>window.location.href = window.location.href.replace('?action=logout', '')</script>
<?php
}
}
?>

Related

How can I get the URL parameter after the domain name in php/apache configuration?

Eg, for a website I have, website.com/abcd, I need to extract the 'abcd' variable and store in a database. If a user visits website.com/abcd it redirects to a 404 page. I have been tinkering with .htaccess but not getting anywhere.
Pop the below into a .htaccess file...
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymlinks
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteRule . /index.php [L]
</IfModule>
... then use <?php print_r($_SERVER);?> to show variables that your server will let you use.
The above htaccess code is pretty basic; if the file/directory that's being requested doesn't physically exist, requests will be redirected to ./index.php.
Add a custom error-handler instead of the builtin to your .htaccess:
ErrorDocument 404 /handle_404.php
In the handle_404.php you can do access all information you need:
<?php
header("HTTP/1.0 404 Not Found");
echo "The url you requested (${_SERVER['REQUEST_URI']}), does not exist.";
// do whatever else you want (e.g. persist to DB)

PHP htaccess wrong with URL's

http://localhost/customers/website/jobs/28
When for an example the URL above is visited and my code is used, it goes to: http://localhost/customers/website/jobs/startpage
if(isset($_COOKIE["startpage"])) {
}else{
header("Location: startpage");
}
But if I do header("Location: /startpage"); it becomes http://localhost/startpage
My Htacess is:
RewriteEngine On
RewriteBase /customers/website/
RewriteRule ^startpage/?$ startpage.php [NC,L]
How can I make it go to http://localhost/customers/website/startpage?
Don't use PHP for this. You can do this in htaccess itself:
RewriteEngine On
RewriteBase /customers/website/
RewriteCond %{HTTP_COOKIE} !startpage
RewriteRule !^startpage/?$ startpage [L,NC,CO=startpage:1:%{HTTP_HOST},R=302]
RewriteCond %{HTTP_COOKIE} startpage
RewriteRule ^startpage/?$ startpage.php [L]
and remove your header function call from PHP code.
If you will want to do this in PHP itself then use:
if(isset($_COOKIE["startpage"])) {
//
} else {
header("Location: " . $_SERVER["BASE"] . "startpage");
}
and keep same rewrite rule shown in question.

PHP - .htaccess and GET parameters

I am trying to create a simple redirect to another page on login script.
Currently I am rewriting my URLS to the following format using .htaccess:
RewriteRule ^login$ ?i=l [L]
RewriteRule ^login([^/]*)$ ?i=l&=$1
The normal URL is therefore: domain.com/login - although I wish to be able to do the following: domain.com/login&redirect=/account/settings (Where the "redirect" will be the $_GET parameter that I'll be redirecting to after successful login)
My problem is if I access the above URL I get a 404 page not found. What am I doing wrong?
Try these rules:
RewriteRule ^login/?$ ?i=l [L,QSA]
RewriteRule ^login(&[^/]+)/?$ ?i=l$1 [L,NC]
Though I suggest using:
domain.com/login?redirect=/account/settings
and get rid of 2nd rule altogether.
QSA flag in first rule will add redirect=/account/settings query parameter as $_GET to your php file.
I use a .htaccess.
It's more functional and simple. I do not know if that's what you need but the handling is very simple.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?param=$1 [L,NC]
Url Example :
domain.com/login/account/settings
The treatment is in the script PHP
$param = $_GET['param'];
echo $param;
Return
login/account/settings
Or then
$param = $_SERVER['REQUEST_URI'];
// (PHP 5 >= 5.2.0)
// $param = filter_input(INPUT_SERVER, 'RESQUEST_URI');
$param = explode('/',$param);
print_r($param);
Return
Array( [0]=> [1]=>login [2]=>account [3]=>settings)
With this method it is possible to accomplish several goals.

custom .htaccess rewrite rules

I want to redirect all to one script e.g. index.php?url=inputurl
With if/else I want to parse url
in index.php run query for url in my custom table
if url is mach: echo "ok"
else do nothing
How should I set .htaccess in root folder of Wordpress?
Example:
URLs in custom_table:
asd
dfg
ghj
If user puts:
www.mysite.com/asd
-> mod_rewrite should output this: www.mysite.com/index.php?url=asd
Else if user puts:
www.mysite.com/zzz
-> do nothing
I think the following .htaccess should solve the problem:
RewriteEngine On
# Redirects everything that is not index.php to index.php
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php?url=$1 [L,R]
Edit: to not include your folders and files (like /js, /css, etc.) in rewrite, add the following lines before the RewriteRule line (see comments):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
And in the PHP script:
$url = $_GET['url'];
// the method is_valid should check if the page exists in DB
if (is_valid($url)) {
// do something here
// maybe redirect with header('Location: path')
} else {
// show a not found page (error 404)
}
You want to both be able to read from a database and do nothing if there is not match.
This would require you run code to access db then return back to apache to process and is not possible from .htacccess (though it is from httpd.conf).
The .htaccess solution would be to specify all the "table" entries inline as below.
RewriteEngine on
RewriteBase /
#if asd or dfg or ghj
RewriteCond %{REQUEST_URI} ^/(asd|dfg|ghj) [NC]
RewriteRule . index.php?url=%{REQUEST_URI} [L]

PHP: htaccess default variable

I want to set a default variable for a query that comes at the end of a url
The .htaccess file redirects the url as follows:
http://www.server.com/folder/subfolder/index.php?page="some-page-name"
displayed url
http://www.server.com/folder/some-page-name
if the page name is not set, as in:
http://www.server.com/folder/
the default would be "index". I could use php function header("location:url") but that would display the "index" part at the end if the url... that I don't want.
htacess content
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^.*\.css.*$ [NC]
RewriteRule ^(.*)$ subfolder/index.php/?page=$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
ErrorDocument 404 /error.html
ErrorDocument 403 /error.html
</IfModule>
You don't have to redirect to index.php. You can use something like:
header('Location: /folder/front-page');
If you just want http://example.com/folder/ show up your index page, you could use the following in your PHP script:
$requested_page = filter_input(INPUT_GET, 'pageID');
$allowed_pages = array('some-page', 'some-page-name');
if($requested_page == ''){
// display your index page as ?pageID is not set or empty
}
elseif(in_array($requested_page, $allowed_pages)){
// display $requested_page
}
else{
// display a 404 Not Found error
}
Try this code in your PHP file:
$pageID = 'index';
if(isset($_REQUEST['pageID']) && !empty($_REQUEST['pageID']))
$pageID = get_magic_quotes_gpc() ? stripslashes($_REQUEST['pageID']) : $_REQUEST['pageID'];
// Your code should now use the $pageID variable...
What this will do is set $pageID to a default value of "index". Then, if a different PageID is given by the mod_rewrite, $pageID will bet set to that value. Your code should then use the value of $pageID.
Your question was too long, didn't read, however if you just want to set a variable in your .htaccess file and pass it to PHP it's pretty easy to do it like this -
In the .htaccess file (http://httpd.apache.org/docs/2.0/mod/mod_env.html#setenv):
SetEnv default_url http://my.url/goes/here
In your PHP script (http://www.php.net/manual/en/reserved.variables.environment.php):
header('Location: '. $_ENV['default_url']);
Alternately, if you have ErrorDocument handlers, you might be able to simply send a status code as well (see the third option to header(), http://us.php.net/manual/en/function.header.php).

Categories