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.
Related
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 have an issue with correctly modifying my .htaccess file.
The goal is to implement a web service on my server to handle Apple's passkit requests.
This works perfect.. almost. Because my intention was, that when a user is typing: www.domain.com he should be directed to www.domain.com/index.php. This would be the usual index.php I would like him to see, when accessing my website.
And when Apples Server is trying to send requests to my web service, they should be redirected to the subfolder /server/index.php. This works fine, when calling www.domain.com
But when calling any different URL, that should be redirected, I get an error:
"Too many redirects occurring.."
I expected the redirection to /server/index.php
How do I have to modify my config file correctly?
I configured the file as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
# If file with specified name does not exist, procede to rewrite rule below
RewriteCond %{REQUEST_FILENAME} !-f
# if request = root
RewriteRule ^/?$ /index.php
# else
RewriteRule !^/?(index\.php)$ www.medifaktor.de/server/index.php [R=301]
# Check for HTTP Header Authorization and if so, import it as a server environment variable into PHP
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] [L]
</IfModule>
Your second rule is wrong since it is checking for not equal to /index.php and redirecting to /server/index.php.
Try this code:
RewriteEngine On
# If file with specified name does not exist, procede to rewrite rule below
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# if request = root
RewriteRule ^/?$ /index.php
# else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^/?(server/index\.php)$ /server/index.php [NC,L]
# Check for HTTP Header Authorization and if so, import it as a server environment variable into PHP
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] [L]
Thank you anubhava! I just tried what you suggested and it works fine. But now something interesting is happening. To test if the URL with the components that don't physically exist will be redirected correcty, I tried the following:
<?php
require_once("lib.php");
// get the request parameters out of the request URL
$requestURL = (($_SERVER['REDIRECT_URL'] != "") ? $_SERVER['REDIRECT_URL'] : $_SERVER['REQUEST_URI']);
$scriptPath = dirname($_SERVER['PHP_SELF']);
$requestURL = str_replace($scriptPath,"",$requestURL);
$requestParts = explode("/", $requestURL);
echo $requestURL;
This should output the components of the URL that is requested by my web service and should split it into its components. But whatever URL I try: I always get the result: /index.php
but there should be the components requested with the URL I think be shown?
I think, I just got the answer by accident:
By switch REDIRECT_URL to REQUEST_URI in the code, I got the correct output.
I have an index.php and news.php and .htaccess files in my localhost/DIRECTORY/AID/
folder, and I am basically trying send/receive data from index.php to news.php
This is a function inside the index.php, which creates a link from database query, and echos out a title of an article.
function news_preview() {
$query = "SELECT * FROM updates ORDER BY update_id DESC LIMIT 5 ";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$url = "/news/$row[update_id]/" . preg_replace('/[^a-zA-Z0-9-_]/', '_',
$row['update_title']);
echo " " . substr($row['update_title'], 0, 26) . "...<br/>";
}
}
echo news_preview();
Now, here is what the .htaccess looks like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]
Now, to the problem. Basically, when I clicked on the link (generated by news_preview() )
shown in the index.php, All I get in the news.php page is nothing. But, probably because I am trying to use the $_GET['title'] Although, I am not certain if that is how we retrieve data. But, the links take me to http://localhost/news/46/This_is_news_title
which is perfect, but I am getting the Object Not Found error
Below, is the image of the error I am getting.
Put this code in the htdocs/.htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^news/([0-9]+)/.*$ /DIRECTORY/AID/news.php?id=$1 [QSA,L,NC]
In the AID/ folder along with index.php & news.php The problem, is I don't know how to get the data from the url in the news.php
The htaccess file needs to be in your document root. When the request URI is in the form:
/news/1234/something-something
The order apache uses to resolve whether overrides (i.e. stuff in htaccess files) should be applied is first see if this is a directory /news/1234/something-something and if so, if there's an htaccess file in it. That's not a directory so apache moves on. If /news/1234 is a directory, and if so, see if there's an htaccess file in it; since it's not, nothing happens. Then apache checks if /news is a directory and if so, check for htaccess; it's also not a directory so nothing happens. Finally, apache checks the document root / to see if there's an htaccess. Since the document root is a directory, that's where you need to put your rules.
The /DIRECTORY/AID/ directory is never in the mix here, unless that is actually where your document root is. If DIRECTORY/AID/ is your document root, e.g. the URI / maps directly to that directory, then you need to change your rules to:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ news.php?id=$1 [QSA,L]
Sounds like you're expecting to be able to read the last portion of the URL as $_GET['title'], but your htaccess rule isn't adding it to the query string.
Try changing
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]
to
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1&title=$2 [QSA,L]
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]
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).