I have a directory site and its sub-folder and files in it.
It also contain a .htacess and below is the code
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?cc=$1
so my below url will is effective and working good.
http://localhost/site/sweetinc
And below code in index.php below
<?php
if (isset($_GET['cc']) and !empty($_GET['cc'])) {
echo $_GET['cc'];
} else {
die("Sorry wrong code");
}
?>
And this is working good.
Now, i want to access or display all files as normally
http://localhost/site/sweetinc/home.php
http://localhost/site/sweetinc/test.php
home.php and test.php are located in site directory. And if it redirects to other sub-folders then it should
be visible like
http://localhost/site/sweetinc/sub-folder1/test3.php
Is this possible, as I am working seperating a group using seperate directory using .htaccess and considering all files in site directory as base files
The trick here (if I understand your question correctly) is that you don't want the rewrite rule to be in effect when you're trying to reach a file that actually exists, right?
In that case, just before your RewriteRule, add these lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
That way, the rewriterule only takes effect when the url (/site/sweetinc for example) does not really exist on the server. If it does (/site/sweetinc/home.php) the rewriterule is skipped and the file is shown.
(-f checks if the filename exists, -d checks if the directory exists, so /home/sweetinc/somedir/ should work too)
Update based on updated question
You need two separate rules for this. First of all, if the /sweetinc/ directory in the url is used, refer them to the /site/ folder:
RewriteRule ^sweetinc/(.*)$ /$1 [L]
Then, if the file does not actually exist, let the index.php handle it:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cc=$1 [L]
Some examples:
I have the following files in my files subdomain:
/index.php (which has the same content as yours does, so it reads out ?cc=
/circle3.PNG
/the .htaccess with the above rules
http://files.litso.com/sweetinc/hello shows the index.php that echoes "hello"
http://files.litso.com/sweetinc/circle3.PNG shows the actual file in /
(note, I don't have a /sweetinc/ directory, it's all faked)
Related
I have set up a mod_rewrite in a sub-directory (NOT root directory) using .htaccess file, code as follows:
#Enable the mod_rewrite
RewriteEngine On
#Rewrite condition as follows, if the URL matches a read directory, file or link then don't do the rewrite.
ReweiteCond %{REQUEST_FILENAME} !-d
ReweiteCond %{REQUEST_FILENAME} !-f
ReweiteCond %{REQUEST_FILENAME} !-l
#The Rewrite rule is as follows:
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
The plan is if the user enters a wrong URL a 404 error to be returned. I have used the following PHP code that worked with me perfectly in a PHP file located in the root directory.
http_response_code(404);
include("404.php");
die();
However, the result is not expected, the page appears as follows:
L���O�����;�γ}��Li[zf˫9]�!i$a����_��ZBU)��,[#y�O$�������'��3�I�m���K���J,b����u�������&�����Im���:�=�A���A���$�d&:����4�`L8�?��ѾQ|��~��"֡2\�Uq�^��~ӫ�=��hO�\uJ��ӳ��0.�L�e��HgtX7��3:l����x�~�#�!Jgt�z3
����ϼ�T�$O�'�6�~�������.��s~A�e�� e�=ذ_2����wfԢ��,��,-�
)�3�3S��Ll���u��\��%[�5['�U
Ϸ�Ƅ3g�5�S��q�|�&-G����-6��O<��uƛ.��y���w�v(W*���Yʁ��7��r�j3q������c�~�g��Yฺ"$ˊ�1cv����;�C��Ҹ3�
h��<�>:��5xޡsP/u㾾
I'm not sure if this has anything to do with the .htaccess, but I have put everything that I thought is related.
Please note that both pages either the one that calls 404.php and the latter has the charset=UTF-8
I'm trying to rewrite all requests to index.php and then there decide which file to include depending on the value in the $_GET['p'] variable. For example I have a script called update.php in the home directory of my site called leltar, which I would like to be included if the opened page is localhost/leltar/update.
However, the problem is that the rewriting does not work because WAMP runs the script even though the .php extension is not in the link. The output is just the one from the script, nothing is shown from index.php. How can I stop WAMP from running the script with similar name? I suppose, there is something wrong with my .htaccess code as well because if I open pages other than localhost/leltar/update, the value of $_GET['p'] is the string "index.php" all the time.
.htaccess file:
RewriteEngine On
RewriteRule ^(.*)$ index.php?p=$1
index.php:
// ...
switch ($_GET['p']) {
case 'update':
require_once('update.php');
break;
default:
// something else
break;
}
// ...
The content of update.php is not relevant.
EDIT:
The main problem is that by default or even when I use the RewriteCond %{REQUEST_FILENAME} !-f rewrite condition if there is a file with the same name as the user-friendly end of the link, WAMP somehow overrides my rewrite rules. If I put a picture named pic.jpg into the www folder and open localhost/pic.jpg, the image shows up, obviously. However, if I leave out the extension visiting the page localhost/pic, I get the same output as well (instead of getting a 404 Not Found error because of the non-exisiting folder, I suppose).
EDIT 2:
On a real server there isn't any problem. If I leave out the extension a 404 error is thrown, so it's definitely a WAMP-specific thing.
Rules in .htaccess files effectively loop until the URL does not change (because the processing is restarted each time, which means the rules are also processed again) so of course your rule loops until p is equal to itself.
You don't actually need p, you can just check $_SERVER['REQUEST_URI'] in your PHP script and that will tell you the original requested URI (not the same as REQUEST_URI below, which does change with each rewrite).
So just use this:
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^ index.php [L]
But... you probably want files that exist to be served, such as images, scripts etc. so the usual thing is to do this and check if the file exists:
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Although it's more efficient to do this and list the directories with files in them that you want to be served:
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !^/(?:images|css|scripts)/
RewriteRule ^ index.php [L]
Just change the list to the names of the directories that have your static files in.
Update
From the comments, it seems you have another rewrite that is adding .php to URLs, so they can work without it. In order to not rewrite them to index.php, I suggest putting the rewrite before this one. Otherwise, you can check if the URL exists with .php on the end of it like this. Perhaps combining it with the folder check rather than having two file-system checks.
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteCond %{REQUEST_URI} !^/(?:images|css|scripts)/
RewriteRule ^ index.php [L]
You can add specific file exceptions to the third rule like this:
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !^/(?:images|css|scripts)/
RewriteCond %{REQUEST_URI} !=/some/url.php
RewriteCond %{REQUEST_URI} !=/some_other_url.php
RewriteRule ^ index.php [L]
Indeed, as #SuperDuperApps assumed, the problem was that WAMP has MultiViews enabled by default. Adding Options -MultiViews to my .htaccess file solved all my problems, my initial code mentioned in the question works fine now.
Im trying to have a htacess file within my root folder so i can redirect the user to index.php file, if for example trying to access any file inside my root directory. I failed to do that. I have that file inside my root folder as i should, but when im trying to access files typing the names in the URL, i actually have access to the files. So the .htaccess file does not work.
I have to mention that im trying to do that locally, having wamp installed and using slim framework. I do not know if something mess with these.
The code that i have in my .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
I have that file inside my root folder as i should, but when im trying to access files typing the names in the URL, i actually have access to the files
That means you want even the existing files not to be shown, and all requests should be redirected to index.php.
If that is the case then why do you have these conditions? Remove these
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
Those 2 conditions mean apply that redirect only if the requested url is niether a file nor a directory. No wonder for files that are actually present your redirect is not working. You told it so.
Hi, so this may be asked elsewhere but I have searched and come up with irrelevant results.
I clearly don't know what to search for exactly.
I'm just trying to rewrite everything after a certain directory to that directories index.php.
Here is an example of the URL a visitor would SEE
website.com/search/location/United%20States
And I would like that URL to be rewritten server-side so that it loads website.com/search/location/index.php
(not a 301 redirect)
I would like the Url to stay the same but load the index.php script (to include United%20States so this can be passed to PHP to determine what the location is and if it is legitimate etc.).
Sorry I know that this will be somewhere already but I just can't find it
I have some code already but it is buggy and seems to choose when it wants to work and also sometimes uses location/index.php/United%20States which is not what I want.
Put this code in your htaccess (which has to be in your root folder)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/location/.+$ /search/location/index.php [L]
If you have Apache web server, make sure you have mod-rewrite enabled and put .htaccess file into your WEBROOT/search/location directory. Put this into .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
This will internally redirect all requests, where file or directory does not exists, to index.php.
You could also put .htaccess file into your WEBROOT directory and write this into to:
RewriteRule ^search/location/.* /search/location/index.php
Hope this helps.
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]