First of all , I clarified with my hosting company and they have verified that Allow Override setting is enabled.
Second if I write garbage value in .htaccess file, then I get "Internal Server Error" which further proves that .htaccess is not skipped.
I am trying to do this
Input : all/3
output: all.php?id=3
I wrote following in .htaccess file, which I copied from a tutorial site.
# Enable Rewriting
RewriteEngine on
# Rewrite user URLs
# Input: user/NAME/
# Output: user.php?id=NAME
RewriteRule ^all/(\w+)/?$ all.php?id=$1
all.php file
<?php
echo $_GET['id'];
?>
Now if I enter localhost/all/3 then I get 3 displayed.
If i enter www.mydomain.com/all/3 then the page is blank.
Also I have only the above code in .htaccess file and nothing more or less. So why I am getting different behaviour in localhost and in my server?
try this :
RewriteRule ^all/(.*)$ all.php?id=$1
Sounds like your host has enabled the MultiViews option. Solutions are to get them to disable it or probably a quicker option, rename your PHP file to be something other than all.php, just so long as the basename (minus the extension) does not match your RewriteRule pattern.
Related
I doubt even it is possible or not. I have a strange requirement by client so have to do.
I have a website that allow certain ips(ips are hard coded in .htaccess ). Client has provided me a url that have some ips listed in json format. I want that these ips should be allowed to access the website. (Ips listed on url are dynamic so i can't use them in .htaccess). Do anyone have idea how to achieve this.
I know i can use a php file(index) in htaccess and write code in it to read ips but the priority is to get it done by .htaccess file only.
Thanks in advance.
Need little bit help.
I have created rewriteMap in httpd file.
RewriteMap accessmap "txt:E:\htdocs\myfolder\map.txt"
My map.txt have follwing entries.
127.0.0.11 allow
127.0.0.1 allow
127.0.0.12 deny
These entries i am adding in htacces file
RewriteCond ${accessmap:%{REMOTE_ADDR}} allow [NC]
RewriteRule ^(.*)$ https://www.exampleweb.com/$1 [L,R=302]
Now, for example i have 127.0.0.1 in my map file as allow so it should be allowed to access the site. If i want to access site form any other ip like
130.0.0.11 (not in map file) it should be redirect to this link https://www.exampleweb.com .
I do not have much knowledge about htaccess to can't understand how to write
rewriteRule and rewrite condition for this. Please help.
Note: Ips are just for example, actual ips will be different for these.
Create a RewriteMap. Add below code in Apache configuration file (httpd.conf) or in your virtual host file.
RewriteMap accessmap "txt:file/path/to/map.txt"
NOTE: RewriteMap directive cannot be added in .htaccess file or inside Directory section. [Source: Using rewrite map]
Then in your .htaccess file add below code
RewriteCond ${accessmap:%{REMOTE_ADDR}} !^allow
RewriteRule ^(.)$ - [F,L]*
This will allow IP addresses listed in map.txt file having value allow to access your site. Otherwise, users will see 403 forbidden error.
.htaccess files are constantly read by Apache HTTPD server. You can be changing the specific Authorization directives in it according to your needs by a script or something else and the new auth directives will be applied instantly.
Whenever I have a file in my webserver and rewrite rule for the same path in my .htaccess, the rule is ignored and the file is automatically served.
I have only one file in the public_html of my webserver - test.php as follows:
<?php
echo "hello ".$_GET['action']
?>
Initially my .htaccess is empty and so visiting http://<domain-name>/test.php?action=world echoes hello world as expected. Also my webhost is so configured that visiting /test?action=world (with empty .htaccess) also echoes hello world.
Now I add the following .htaccess in public_html:
RewriteEngine On
RewriteRule ^test/(\w+)$ test.php?action=$1 [NC,L]
I expect that visiting /test/world will echo hello world, but that does not happen! I get only hello in the response. Now my initially thought is that mod_rewrite is not enabled, so to test that I change the .htaccess to:
RewriteEngine On
RewriteRule ^testmod/(\w+)$ test.php?action=$1 [NC,L]
Now to my surprise visiting /testmod/world actually echoes hello world! This shows that mod_rewrite is indeed available, and confirms that the rewrite rule is ignored when a file actually exists for the requested url.
My mental model is that Apache goes through the rules in the .htaccess, and for any rewrite rule matching the requested rule, Apache internally redirects to path specified in the rule. But that does happen here. It looks like first it checks if there exists any file for the requested url, and if not present, then look at the rewrite rules. Or something entirely else is happening here, maybe because of my webhost's configuration? (FYI, I'm using Bluehost as my webhost).
Add this to disable MultiViews:
Options -MultiViews
The Apache docs on mod_negotiation, describes what the Multiviews Option does, when enabled:
If the
server receives a request for /some/dir/foo and /some/dir/foo does not
exist, then the server reads the directory looking for all files named
foo.*, and effectively fakes up a type map which names all those
files, assigning them the same media types and content-encodings it
would have if the client had asked for one of them by name. It then
chooses the best match to the client's requirements, and returns that
document.
Use:
Options -MultiViews
RewriteEngine On
RewriteRule ^test/(\w+)$ test.php?action=$1 [NC,L]
Update I have re- done the code and folder system of my mod re write on localhost still nothing
the link only shows like this
http://localhost/cms/member/profile.php?member=10308
my entire .htaccess file
RewriteEngine On
RewriteRule ^cms/member/([^/]*)$ /cms/member/profile.php?member=$1 [L]
when i click on the link it does nothing. if i change in the browser to
http://localhost/cms/member/10308
it says page cannot be found.
Update the problem was this peice of code causing it to not display right
<?php
if(empty($_GET['member']) || $_GET['member'] <1000 ) {
redirect(ROOT_URI);
exit;
}
?>
But i need this code.
the url will work but i need to type it in manually it wont automatically change the url
As you are using an .htaccess file which is placed under the directory "cms", you should use the following directives:
RewriteEngine On
RewriteRule ^member/([^/][0-9]*)$ member/profile.php?member=$1
This will send the request http://localhost/cms/member/(any-number) to http://localhost/cms/member/profile.php?member=(any-number)
Note:
mod_rewrite should be enabled.
In httpd.conf (or apache2.conf), replace AllowOverride None by AllowOverride All to enable .htaccess
Restart Apache server.
I've had to move from testing on the live server, to testing locally on a virtual apache server. I've installed XAMPP just fine, downloaded and installed the wordpress files and the database. Everything looks great! The local version of my homepage is identical to the live version. There's only one problem: the homepage is the only page that works. When I click on one of the links i.e. the "about" page (http://localhost/wordpress/about/), I am redirected to the xampp control panel (http://localhost/xampp).
I have a good feeling this has to do with a problem with the "pretty links"/mod_rewrite rules. I made sure I brought over the .htaccess file, and it contains the rewrite instructions. The wordpress database has the proper permalink structure, and the httpd.conf file has the "RewriteEngine on" and the "FollowSymLinks" directives enabled. There has got to be some sort of rewrite problem here, although I am not ruling out something else stupid I might have done. Thanks for all your help!
-E
*Here is what the .htaccess looks like:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
In the httpd.conf, change the
DocumentRoot "/path/to/your/app/wordpress"
also
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/path/to/your/app/wordpress">
This should work, the path is absolute.
And do you have load the:
LoadModule rewrite_module modules/mod_rewrite.so
in httpd.conf??
If your server version works with a domain name, i.e: http://domainname.com is equivalent that http://localhost/wordpress the rewrite rules will be differents. post the rules here.
The problem is that wordpress does not believe in relative paths for some reason. There is an assumption that wordpress is running from the server root not a directory under the root (e.g. /var/www/wordpress will not work, but /var/www/ will).
The problem is with the .htaccess file they provide. It should re-write it to index.php and not /index.php. Change that line in your config and it will work.
What happens is that it tries to actually go to the default document root (in my case /var/www/index.php, which does not exist since I am using http://localhost/worpress which is an alias for ~/projects/worpress). You can check your error log and it will tell you where it is trying to look for the index.php file (which will return a 404 error).
I can go on a rant about how stupid it is that they do that and how bad the whole software design of wordpress is. But I will spare you that :).
Say I have a rewrite that needs to pass the url to a PHP function and retrieve a value in order to tell what the new destination should be? is there a way to do that?
Thanks.
UPDATE:
Thanks so far guys..I am still having trouble but i'll show you my code:
.htaccess
#I got the file path by echoing DOCUMENT_ROOT and added the rest.
RewriteMap fixurl prg:/var/www/vhosts/mydomain.com/httpsdocs/domain_prototype/code_base/url_handler.php
RewriteEngine On
RewriteRule (.*) ${fixurl:$1} [PT]
PHP:
set_time_limit(0); # forever program!
$keyboard = fopen("php://stdin","r");
while (1) {
$line = trim(fgets($keyboard));
print "www.google.com\n"; # <-- just test to see if working.
}
However I am getting a 500 Internal Server Error I am not sure if there is an error in my .htaccess or in my PHP?
There is something called a RewriteMap.
You can call an executable script that will return the URL to rewrite to.
Check this article for more information and examples (in Perl, but are totally applicable to any other language):
http://www.onlamp.com/pub/a/apache/2005/04/28/apacheckbk.html
Summary of caveats:
Must run a read STDIN loop (ie, don't exit after receiving an URL)
Must print the URL to rewrite to WITH a trailing newline
Must be readable and executable by the user Apache runs as
This is the way to create the map
RewriteMap fixurl prg:/usr/local/scripts/fixit.php
And now we can use it in a RewriteRule:
RewriteEngine On
RewriteRule (.*) ${fixurl:$1}
EDIT: About the Internal Server Error. The most probable cause is what Gumbo mentions, RewriteMap cannot be used in .htaccess, sadly. You can use it in a RewriteRule in .htaccess, but can only create it in server config or virtual host config. To be certain, check the error log.
So, the only PHP / .htaccess only solution would be to rewrite everything to a certain PHP program which does the checking and redirects using the Location header. Something like:
RewriteRule (.*) proxy.php?args=$1 [QSA]
Then, in proxy.php
<?php
$url = get_proper_destination($QUERY_STRING); #args will have the URI path,
#and, via QSA you will have
#the original query string
header("Location: $url");
?>
Yes; you need a RewriteMap, of the External Rewriting Program variety.
What I would do is pass the URL to the PHP page and then do the final redirect using the PHP headers option.
header("Location: new-page.php");
If you didn't want it to redirect, you could also do an include of the page you want. I find that this is a little more flexible than using RewriteMap.
Hope this helps!
Note that the RewriteMap directive can only be used in the server config or virtual host context.