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]
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 am trying to change my website URL according to get variables so that I can increase the security of my website.
For example I want to change my address, this is my htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /category.php?cat_id=$1&mode=full&start=$1
And my website URL is:
http://joinexam.in/category.php?cat_id=17&mode=full&start=36
I want to convert this URL to:
http://joinexam.in/category/1736
where cat_id = 17
and start= 36
So it will become 1736 after the category.php page, I am trying to do it by using .htaccess file.
Here I want to take both cat_id and start as get variable, then according to these get variables, I want to change the URL of my website.
Can anyone explain the correct way to achieve this?
.htaccess
This forwards every URL to index.php.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
PHP
Get the "original" URL inside index.php:
// this gives you the full url
$request_uri = $_SERVER['REQUEST_URI'];
// split the path by '/'
$params = split("/", $request_uri);
Then you might route based on these $params.
As a good and fast router lib i suggest: https://github.com/nikic/FastRoute
By using this, you don't have to mess around with htaccess regexp stuff and can keep things at the PHP level :)
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)
Okay...My title is a bit of an exaggeration...
My site is built in PHP, and all the files I'm trying to "require_once" aren't being loaded. The only file I've changed is my .htaccess file. I don't know a thing about .htaccess and what I have is purely from searching the web. What is wrong? Thanks for any help in advance.
RewriteEngine on
<Files 403.shtml>
order allow,deny
allow from all
</Files>
ReWriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteRule !index.php index.php [L]
Also, if I comment out the bottom two lines, my site works great.
Well, require_once has nothing to do with .htaccess file: it's a PHP directive, not an Apache one. You have to set correctly the include_path for your files and make sure these directories and files are reachable (i.e., with correct privileges set on them).
If you show the error message you got from failed require, it'd be much more simple to give you a specific advice on how to fix it.
UPDATE If what you need is redirecting all the non-AJAX requests for .php files into index.php, your .htaccess should like this:
RewriteEngine on
RewriteCond %{HTTP:x-requested-with} ^XMLHttpRequest$
RewriteRule . - [L]
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteRule .php$ index.php
This basically means the following: "all AJAX requests - go for what you need, all non-AJAX requests IF you're not going for some directory and are ended with .php - go for index.php instead".
Without checking for .php (or some similar check) you will redirect to index.php all the script loading procedures; and, unless you do it from some external CDN, it's not what would work in your case. )
Try changing the last two lines to this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
If you want your URL's to look something like this (you probably do):
http://yoursite.com/some/path/somewhere
then change the last line to:
RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2
If that's what you want to achieve, ensure that if you're trying to go to:
http://yoursite.com/about
That there isn't actually a folder called about, this line:
RewriteCond %{REQUEST_FILENAME} !-d
Checks to see if a folder with the name "about" exists, if it does, then the page will not redirect, the same goes for files, say you go to:
http://yoursite.com/about.html
If about.html actually exists then the page will not redirect.
Hope that makes sense.
If you need more information, http://jrgns.net/content/redirect_request_to_index seems to be fairly succinct and helpful.
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]