.htaccess with database - php

How can I do this that mydomain.com/ -> mydomain.com/profile.php?username=
Now You can start search via url like domain.com/
My htaccess file is
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?s=search&val=$1 [QSA,L]
I can understand in php file
$val = $_GET['val'];
if($c->userNameCheck($val))
{
header("Location:".$url."/profile.php?username=".$val);
}
But I dont want to show profile.php

You could simply replace the rule with:
RewriteRule ^(.*)$ profile.php?username=$1 [QSA,L]
However, since you won't be executing the index.php file, the userNameCheck method won't be called. I assume it does some sort of a check if the provided parameter is a valid username -- to make it run with the modified rule, you'd have to move that call to the profile.php file.

Related

Is there a way to ignore RewriteRule if the script executing is a PHP script on the server?

I have a rewrite rule intended to route the users to my index.php if they visit my /images/* directory so that I can route this correctly and check user permissions before displaying the image.
However, the issue here is that when I try to display the image to the user, the call to display the image is being picked up by the RewriteRule too.
Is there a way to ignore the RewriteRule if the file executing is PHP/phtml?
My htaccess
RewriteEngine on
RewriteRule (/)?images/(.*)$ /index.php?request=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
I'm looking for something like
if the file executing is not a PHP script/being called from the server:
RewriteRule (/)?images/(.*)$ /index.php?request=$1 [L,QSA]
You can use a RewriteCond to check if current request is not for .php:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteRule (/)?images/(.+)$ /index.php?request=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
You can try below code :-
.htaccess code :-
RewriteRule (/)?images/authorize/(.*)$ /images/$1 [L,QSA] // Change path or name according to you
RewriteRule (/)?images/(.*)$ /index.php?request=$1 [L,QSA]
PHP code :-
check for user authorization.
if ($user) // Authorized
{
redirect ('http://project_name/image/authorize/test.jpg'); //// Change path or name according to you
} else {
echo 'Not Authorized';
}
It may help you.
I'm not too proficient in url rewrites. but i think you might want to try using 2 REWRITE_COND directives. one for the checking the IP of requester, and the other for the file types.
this might be useful
"REMOTE_ADDR
The IP address of the remote host (see the mod_remoteip module)."
so what i am saying is, maybe you can check the IP and allow the IP of your own webserver(first cond) to access the images(for this you need the second cond). and you should be good to go.

how to get the url entered from address bar when it is entered manually after domain name in PHP

Assuming my website name is www.example.com which is written in php.
When I type www.example.com is displays the page. But when I type www.example.com/content/somepage.html it says 404 page not found which is correct since it doesn't exist.
Now if the user types this www.example.com/content/somepage.html manually I shouldn't redirect to error 404 page but rather I need content/somepage.html this value which I can process it further which is my requirement.
Usually index.php is the first page which gets invoked is there any way I can get the value entered after www.example.com
content/somepage.html is just an example but it can be anything. So what ever is present after www.example.com I need to fetch it.
Sample Download Link
I am using wamp and these 2 files are present in www root folder
index.php
<?php
echo 'The Uri is : '.$_SERVER['REQUEST_URI'];
echo '<br /><br />';
if(isset($_SERVER['path']))
echo 'The parameter is : '.$_SERVER['path'];
?>
.htaccess file
DirectroyIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?path=$1 [QSA,L]
You are talking about can be achieved by using .htaccess for url routing.
Sample
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^content/(\d+)*$ ./index.php?id=$1
Update based on comment.
In last line you can use ...
RewriteRule (.*) index.php?url=$1 [L,QSA]
Inside index.php file you shall be able to get the rest of the string as $_GET['url'];
You can use this rule in your site root .htaccess:
DirectroyIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?path=$1 [QSA,L]
This will populate path query parameter in /index.php with the URI path content/somepage.html.
Or else use FallbackResource:
FallbackResource index.php
and use $_SERVER['REQUEST_URI'] variable in your /index.php to get original URI.

How to stop RewriteEngine/PHP redirect loop?

The task seems very simple:
all requests must be passed through one file.
However,
when following code is added to httpd.conf:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?route=$1 [QSA,R=301,L]
and following code is added to /index.php:
if( isset($_GET["route"]) && $_GET["route"] != "/index.php" ){
header("Location: ".$_GET["route"]);
}
then it causes redirect loop!
Causes for example such loop:
www.site.com/image.jpg -> (redirected by httpd.conf)
www.site.com/index.php?route=/image.jpg -> (redirected by index.php)
www.site.com/image.jpg -> (redirected by httpd.conf)
www.site.com/index.php?route=/image.jpg -> (redirected by index.php)
...
So, the question is following:
How to stop this loop?
To stop for example in such way:
www.site.com/image.jpg -> (redirected by httpd.conf)
www.site.com/index.php?route=/image.jpg -> (redirected by index.php)
www.site.com/image.jpg (no further redirection)
This one is a bit of patch, but I think it works.
httpd.conf:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^(.*)a=a$ [NC]
RewriteRule ^(.*) index.php?route=$1 [QSA,L]
index.php:
<?php
if( isset($_GET["route"]) && $_GET["route"] != "/index.php" ){
header("Location: ".$_GET["route"].'?a=a');
}
The thing is: First time the rewrite engine redirects the page to index.php. Then, in index.php there's a redirection to the same file but adding parameter a=a. Then rewrite engine comes again, but he has orders of not redirect when found the string a=a in the query string, and there's no further redirection.
Of course this will end in a 404 error, since you're filtering only non-existant files with the first two rewritecond, but I guess you know how to deal with that.
Your rule looks fine but R=301 is a bit odd in your rule as you don't want to expose your internal URL in browser.
However PHP code is looking suspect due to this condition:
$_GET["route"] != "/index.php"
Which will always be true since /index.php itself will not be routed through this rule due to these conditions:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
Hence your code will cause a redirect loop by continuously redirecting using header function.
I suggest keep your rule like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?route=$1 [QSA,L]
And your PHP code should just check presence of $_GET['route'] but should not do any redirects.
First, put this to .htaccess file, not in httpd.conf
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [QSA,R=301,L]
Then, remove this redirect from your index.php file. You can get all your variables via $_GET or $_REQUEST.
What a senseless solutions. Of course it will cause redirect loop. In the RewriteCond-s you trying to catch files and dirs names that not exists in the server file system, and then you trying to pass their names to the index.php in which you make to request them again, again and again.
There is a single boilerplate that may come in handy, it should be placed on the top, after RewriteEngine on condition:
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !^[\s/]*$
RewriteRule ^ - [L]
An env was added, it works by theory, however I have tested it only once, see if it can solve the issue.
Why are you putting this:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?route=$1 [QSA,R=301,L]
In httpd.conf? You need to place this into file called .htaccess, create it into your root directory.

How to redirect to .php file if exists and another if it doesn't

I've only just written a forum in PHP. In my database, under my posts table and my categories table, I have a field called 'slug' for the url.
I have urls like /html for my HTML category and /html/test for a post named test under the HTML category.
But I also want urls like /signin which redirect to signin.php.
Is there a rewrite condition I can use that if I go to, for example /something, it will chech if something.php exists. If it does, it will show the content of something.php, else it will show category.php?cat_id=something?
You can use simple php file_exists() function
if(file_exists("require.php")){
require_once("require.php");
}
Is there a rewrite condition I can use that if I go to, for example /something, it will chech if something.php exists. If it does, it will show the content of something.php, else it will show category.php?cat_id=something?
Try:
RewriteEngine On
# exclude legit requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# check if requests is pointing to a php file
RewriteCond %{REQUEST_URI} ^(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
# otherwise, serve the category (also need to exclude legit requests)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /category.php?cat_id=$1 [L,QSA]
You can do the following :
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-]*)(.*)$ /$1.php [QSA,L,E]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /default.php [L]
You can use the file_exists-function in php in combination with the header-function.
This should result in something like:
if(file_exists("file.php")){
header('Location: http://www.example.com/');
}
You can user $_SERVER['REQUEST_URI'] to get your value then use
if (file_exist("[yourvalue].php") {
header("Location: [yourvalue].php");
}
else {
header("Location: categories.php");
}

Using htaccess to remove .php and allow path

I currently have a .htaccess file that allows people to enter the URL without the php extension, such that http://domain.com/account redirects to account.php
I would like to be able to have it so that if I enter http://domain.com/account/contactinfo (or http://domain.com/account/settings/groups and so on) it still goes to account.php, but I am not sure how to change what I have to achieve this.
Current .htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(([A-Za-z0-9\-_\.]+/)*[A-Za-z0-9\-_\.]+)?$ $1.php
Any help appreciated! Obviously if there exists a folder it should follow that path (e.g. if /folder/page.php exists, then http://domain.com/folder/page/create would go to folder/page.php)
Try this is you don't need to pass any URI info into query string (i.e. your app will still look at $_SERVER['REQUEST_URI'])
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\-_\.]+)(/[A-Za-z0-9\-_\.]*)?$ $1.php&q=$2 [QSA]
# Note the optional '&q=$2' on line above if you want to make removed part of URI available as passed parameter
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ / [L,QSA]
Note that since I removed the condition to check for a valid php file, I added a second conditional rewrite rule to just redirect to site root if the re-written request does not point to a valid PHP file. You could obviously redirect this to a 404 page or whatever else you might want to redirect to. Or you could remove this altogether and let Apache give it's default 404 response.

Categories