Problems with Multiviews and redirecting - php

I recently deactivate the Multiview option in my htaccess file.
It did the job perfectly, but we also use an external application that call a link with the extension file (http://example.com/export_me?mode=csv&id=3)
So the link does not work anymore and i want to use a rewrite rule to correct this error (i do not have access to the application)
RewriteCond %{REQUEST_URI} ^export_me?mode=(.*)&id=(.*)\$
RewriteRule (.*) export_me.php?mode=$1&id=$2 [L]
But i still have a 404 error.
Can anyone help me ?
Thx

Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ export_me.php?/$1 [L]
</IfModule>

Why not just simply:
RewriteRule ^export_me$ /export_me.php [L]
The query string will get appended automatically. You can't match against the query string in the %{REQUEST_URI} variable, only the `%{QUERY_STRING} variable.

Related

URL rewriting using php not working

I have my url as
http://public_html.com/sub-products.php?catid=42
I want it to look like
http://public_html.com/sub-products/catid/42
I have made a .htaccess file too. but still there is no change in my url
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^sub-products/catid/([0-9]+)/?$ sub-products.php?catid=$1 [L]
Do i need to change anything in my php file also?
what code is required for php file to use .htaccess file?
Your rule in .htaccess rewrite only incoming seo-pretty URls to URL with php script filename. But if you need construct url as /sub-products/catid/42 you have to change you PHP code, that render HTML output.
for example
while ( $products as $product ) {
echo '<a href="/sub-products/catid/"'.$product['id'].'>'.$product['name'].'</a>;
}
Maybe there is a purpose to your line
RewriteRule ^(.*)$ $1.php
But i can't see it for the need you have, so i guess a
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sub-products/catid/([0-9]+)/?$ /sub-products.php?catid=$1 [L]
should work.
Edit :
This should work if you are trying to access the http://public_html.com/sub-products/catid/42 url
(or http://public_html.com/sub-products/catid/42/ btw)
If what you want is going from
http://public_html.com/sub-products.php?catid=42
to
http://public_html.com/sub-products/catid/42/
You might need something like this :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^catid=([0-9]+)$
RewriteRule ^sub-products.php$ /sub-products/catid/%1/? [R=301,L]
And then, you can use what i said before.
I am not sure if you switched your first statment around, but it looks in your .htaccess file you want to rewrite the other way around. Maybe this would work.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} catid=([0-9]+)$
RewriteRule ^/?sub-products.php /sub-products/catid/%1 [R,L]
If you want to have a more generic rule, and not just for catid this RewriteRule could work:
RewriteCond %{QUERY_STRING} ([a-zA-Z]+)=([0-9]+)$
RewriteRule ^/?sub-products.php /sub-products/%1/%2 [R,L]
Hope it helps you forward.
EDIT: Made some changes after testing it on my system.

.htaccess rewrite php to php file

I am currently trying to write a rewrite rule. I want to simplify the entered URL so that it will always use the index. The input url would be www.example.com/home.php and would be rewritten to www.example.com/index.php?r=page/home.
I wrote this .htaccess file:
RewriteEngine On
RewriteRule ^([^/]*)\.php$ /index.php?r=page/$1 [L]
This configuration unfortunately gives me an error 500. I can understand that it is caused by the fact that if I enter index.php for instance, apache will not know if it must use the index.php file or use the rewritten url index.php?r=page/index.
Is it possible to accomplish this kind of rewriting rule with apache? If so, how can I fix my error 500?
Edit: Please note that the RewriteRule works fine if I change the extension .php to anything else such as .html, as so: RewriteRule ^([^/]*)\.html$ /index.php?r=page/$1 [L]
You have two possibilities.
First one
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule ^([^/]+)\.php$ /index.php?r=page/$1 [L]
Second one
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /([^/]+)\.php
RewriteRule ^.*$ /index.php?r=page/$1 [L]
you can also try with this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?r=page/$1 [L]

How to create mod rewrite rule for simple api

I want to make my first api, but im having trouble setting up the urls. The api url is here:
http://tools.fifaguide.com/api/
So for example if some one goes to:
http://tools.fifaguide.com/api/player/messi
Then I need this page to be loaded:
http://tools.fifaguide.com/api/server.php
What do I write in .htaccess? and where should I put it?
This is what I have right now:
RewriteEngine On
RewriteRule ^api http://tools.fifaguide.com/api/server.php
But it doesnt do anything, also the htacces file is in the api folder.
Any help would be really apreciated!
This is what ended up working for me
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ api/index.php [QSA,L]
////// wordpress stuff /////////
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You might try this:
RewriteEngine On
RewriteRule ^api/(.*) http://tools.fifaguide.com/api/server.php [R,L]
And if you need the resulting url you could add this:
RewriteEngine On
RewriteRule ^api/(.*) http://tools.fifaguide.com/api/server.php?r=$1 [R,L]
Then in your script you could access the requested url with $_GET["r"] (assuming php...)
Also a helpful tool i've found for htaccess:
http://htaccess.madewithlove.be/
-Ken

htaccess rewrite rule to redirect to file or not

I have been trying to write a set of htaccess rules that will do the following:
With the url:
mydomain.com/blog/something
If a file called blog.php exists in
the web directory(the directory with
index.php in) then redirect to
blog.php?url=something
If blog.php does not exist, redirect
to index.php?url=blog/something
EDIT: mydomain.com/blog/something is an example, it could be any url string and the htaccess should search the web directory for a file corresponding to the first part of the url string, which in this case is "blog"
In both cases I can then use $_GET['url'] to get parameters from the url to intiate whatever actions in my php script.
I currently have:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)$ $1.php?url=$2 [L]
RewriteRule ^(.*)$ index.php?url=$1 [L]
</IfModule>
But unfortunately this is not working, I'm not sure why, I'm proficient enough with php but my htaccess skills are pretty limited. I don't know if the [L] option is the one I should be using or if I should be using more options. Currently whatever the url string is, $_GET['url'] returns "index.php".
If any more information is required just ask.
I hope someone can help
Regards
Luke
I hope that the following directives will work for you.
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?.*$ - [E=FILE:%{DOCUMENT_ROOT}/$1.php]
RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} -f
RewriteRule ^([^/]*)/?(.*)$ $1.php?url=$2 [QSA,L]
RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Apache mod_rewrite Reference: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

mod_rewrite Probs

I am quite new to PHP and just getting started with mod_rewrite. I know the basic lingo but come stuck when I want to simply refer to the route directory
i.e. this is not probs
RewriteRule ^settings/$ settings.php
[QSA,L]
But how to for example make:
RewriteRule ^page/(.*)$
index.php?Page=$1 [QSA,L]
which generates /page/[page-name]
Just become
/[page-name]
?
Maybe I didn't understand you but it seems that you need such .htaccess file to solve your problem.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Ignore valid files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Page=$1 [QSA,L]
</IfModule>
This should do it:
RewriteRule ^(.*/)$ index.php?Page=$1 [QSA,L]
However, you should place that rewrite rule after all the other specific rewrite rules you have, otherwise all requests will be redirected to index.php?Page=....

Categories