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]
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 want to do a task like this.
I have a folder /doc/ with many documents files inside. When an user trying to access this folder's content, I need to do some verification, so I need to redirect that access to a download.php file in root folder and pass the actual REQUEST_URI to PHP file. I try this in htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/doc.*$ [NC]
RewriteRule ^(.*)$ http://example.com/download.php?r=%{REQUEST_URI} [L,R=301]
But I got ERR_TOO_MANY_REDIRECTS error with the redirected url is: download.php?r=/download.php, which in this case is not what I want (download.php?r=/doc/abc/xyz.pdf is the correct one)
Could someone explain for me about what I did wrong? Thank you very much.
Your rewritecond line is incorrect and it will continue looping forever. You'll need to add a File check so it doesn't use the rewrite condition every time. So it will only use it when it's pointing to a file that doesn't exist
add the following lines
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^doc\/(.*)\.html$ download.php?r=$1 [L,R=301]
with the url http://example.com/doc/file.html
it will redirect the user to download.php?r=file if you wanted to capture the file extension change the last line to
RewriteRule ^doc\/(.*)$ download.php?r=$1 [L,R=301]
I have my query string to include my pages, in my homepage like you see below and it is working fine, Im including my pages fine.
But something wrong is happening and Im not finding how I can solve this.
I will try to expain my isse with an example: I have a folder "teachers" inside I have two pdf documents and a page "documents.php".
To acess this documents page, Im acessing: "htp://localhost/website/teachers/documents", and it is working fine.
But If I acess "htp://localhost/website/teachers/", Im able to acess my pdf documents and my page as you see in my image below.
But I dont want this, I want that If some user tries to acess "htp://localhost/website/teachers/", I want to include my 404 file (require_once('inc/404.php');)
My query string:
#$url = $_GET['url'];
$url = explode('/', $url);
$url[0] = ($url[0] == NULL ? 'index' : $url[0]);
if(file_exists('inc/'.$url[0].'.php')){
require_once('inc/'.$url[0].'.php');
}
elseif(file_exists($url[0].'/'.$url[1].'/'.$url[2].'.php')){
require_once($url[0].'/'.$url[1].'/'.$url[2].'.php');
}
elseif(#file_exists($url[0].'/'.$url[1].'.php')){
require_once($url[0].'/'.$url[1].'.php');
}
else{
require_once('inc/404.php');
}
Do you see what Im doing wrong to not be having the result I want?
My htaccess file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
There is an easier solution.
Add this line to the beginning of your .htaccess file:
Options -Indexes
This way, you won't be able to see the folder contents.
EDIT: htaccess rule solution (which is in website folder)
ErrorDocument 404 /website/inc/404.php
RewriteEngine On
RewriteBase /website/
RewriteRule ^teachers/$ - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]
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 am trying to get url from:
192.168.0.1/movie-page.php?id=123
to:
192.168.0.1/movie/movie-name
or even (for now):
192.168.0.1/movie/123
I've simplified it by using this url (to get something working):
192.168.0.1/pet_care_info_07_07_2008.php TO 192.168.0.1/pet-care/
my .htaccess file:
RewriteEngine On
RewriteRule ^pet-care/?$ pet_care_info_07_07_2008.php [NC,L]
What am I doing wrong? I've tried many combinations but no luck and my patience is running out...
I am running this on my local NAS which should have mod_rewrite enabled by default. I have tested .htaccess by entering random string in .htaccess file and opening the page, I got 404 error. I assume this means that .htaccess is being used since the page stops functioning if the file is malformed.
If you want to rewrite:
192.168.0.1/movie-page.php?id=123 too
192.168.0.1/movie/movie-name or 192.168.0.1/movie/123
Then you would do something like, but will require you manually add a rewrite for any new route (fancy url) you want, and eventually you may want your script to create routes dynamically or have a single entry point to sanitize:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^movie/([a-zA-Z0-9-]+)$ movie-page.php?id=$1 [L]
So a better method is to route everything through the rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
Then handle the route by splitting the $_GET['route'] with explode()
<?php
//192.168.0.1/movie/movie-name
$route = (isset($_GET['route'])?explode('/',$_GET['route']):null);
if(!empty($route)){
//example
$route[0]; //movie
$route[1]; //movie-name
}
?>
You want something like this:
RewriteRule ^movie/([0-9]*)$ /movie-page.php?id=$1 [L,R=301]
That will give the movie ID version with a numeric ID.