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).
Related
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
}
}
?>
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)
I want to pass username in url just like what other social networking sites like facebook do.
The url should be like : www.mysite.com/username
Also I want to be able to access directories if the value at place of username is a directory name.
Ex. www.mysite.com/downloads will take to the downloads directory.
For this what I can do is to first check where the value passed is a valid directory name. If yes, access directory else check for whether user exists with the username passed.
for this what I have done is, created a .htaccess file in root directory containing
RewriteEngine On
RewriteRule ^ index.php
and checking for the url
// requested url
$request = $_SERVER['REQUEST_URI'];
// trim last / if available
$url = rtrim($request,'/');
// explode url and save in $ar array
$ar = explode('/',$url);
if(sizeof($ar) > 2)
{
header('location : error/?error=1');
} else {
$user = $ar[1];
}
The if statement above is to check for passed parameter is one or more.
Ex. www.mysite.com/username/post will result in an invalid url
But by this code, I couldn't access my directory www.mysite.com/downloads
what is the easy way to do this.
Edit 2 :
I want the url www.mysite.com/username to pass username as variable if is not a directory or file to index.php in myProfile directory where it is access as $user = $_GET['u'];
updated .htaccess
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
Options All -Indexes
# Timezone
SetEnv TZ Asia/Kolkata
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /myProfile/index.php?u=$1 [L,QSA]
Try this code in /.htaccess:
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ myProfile/index.php?u=$1 [L,QSA]
If you still get 404 then verify whether your .htaccess is enabled or not, by putting same garbage (random) text on top of your /.htaccess and see if it generates 500 (internal server) error or not when you visit www.mysite.com/index.php?u=username URL in browser.
I am using an .htaccess file to redirect "http://www.domain.com/cars/cars.php?cars_item=231" to "http://www.domain.com/cars/231/porsche"
So basically I'm redirecting 'cars.php?cars_item=231' to '$id/$title'
The .htaccess file consists of:
#Start
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect cars.php?cars_item=231 to cars/231/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+cars/cars\.php\?cars_item=([^&\s]+) [NC]
RewriteRule ^ /cars/%1 [R=302,L]
# Internally forward cars/231/ to cars.php?cars_item=231
RewriteRule ^cars/([0-9]+) /cars/cars.php?cars_item=$1 [NC,L,QSA]
#end
Then i'm using the following hrefs:
"http://domain.com/index.php" = echo porsche;
and in "http://www.domain.com/cars/cars.php" i'm using a $_GET['cars_item'];
My question is: How could I remove the $id from the url please? I want to have the following url as a result - "http://www.domain.com/cars/porsche" ... so basically I want to update the .htaccess and redirect "http://www.domain.com/cars/cars.php?cars_item=231" to "http://www.domain.com/cars/porsche"
This is because the $id in the url is creating some problems for indexing especially. Google keeps crawling "http://www.domain.com/cars/$id/" where as the $id is not a folder but is a db row hence I'd like to remove the $id from the url.
I would appreciate your help as always.
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]