Htaccess mod_rewrite problem - php

I have problem with this:
RewriteEngine On
RewriteRule ^tuote/([^/]+) tuote.php?data=$1 [L]
it should change all tuote/Something-Something to tuote.php?data=Something-Something in server side (not redirect) but seems not to work ($_GET['data'] gets no value).
And other question:
How could I make this 1-Something-title-nice-title to go php
$first_part = "1";
$secnd_part = "Something-title-nice-title";
tried explode, but it does not work in this case.
----EDIT----
Thank you for replies.
I changed htaccess file to:
RewriteEngine On
RewriteRule ^tuote/(.+) tuote.php?data=$1 [L]
but still $_GET['data'] value is empty. BTW, I have other htaccess in parent directory, maybe it would affect in the code?
The other htaccess file has content:
<IfModule mod_php.c>
php_flag display_errors 1
</IfModule>
AuthUserFile /var/www/somepath/.htpasswd
AuthGroupFile None
AuthName "Kirjaudu sisään"
AuthType Basic
require valid-user
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
And about 2nd question, it started to work. Thank you!

Make sure you do not have directory "toute". If you do, make sure you don't have .htaccess in there which would take precedence.
Create your rule match or sure. RewriteRule ^(.*)$ route.php?data=$1 [L] then see what's inside $_GET['data'], that should give you a clue about why your rule is not oring.
If it's not working, AllowOverride is not working. Confirm by misspelling directive in .htaccess without any effect on apache.
If you do get the string, try replace it with more strict rule.
For your second question, you can do this
list($id,$rest)=explode('-',$_GET['data'],2);
Alternatively you can have your regexp select separate parts of your URL into different variables $2 $3 etc.

In your rewrite, all you're doing is checking for 1 or more occurrences of a forward slash. You need to change it to something like:
RewriteEngine On
RewriteRule ^tuote/(.+) tuote.php?data=$1 [L]
For the 2nd question, looks like you're just trying to make a single string:
$both_parts = $first_part . "-" . $sednd_part;

The RewriteRule seems fine -- make sure you are accessing the query string parameter correctly in your PHP code.
As for your other question, adding a limit to explode should do it:
list($first_part, $second_part) = explode('-', $string, 2);
See PHP documentation for explode for more information.

Related

How to get variable in query string without using "?"

I'm trying to use GET to capture member IDs in a URL without using the ?name=variable. For instance, instead of using mydomain.com/folder?id=1234 I would like to use mydomain.com/folder/1234 and pick up the id "1234" in the code so I can pass it to another URL.
This page simply redirects by using: <iframe src="http://redirect_domain.com/folder/index.php" />
I have no control over the server I'm redirecting to, but need the variable passed to it. I can append the redirect URL with ?id=1234 ie <iframe src="http://redirect_domain.com/folder/index.php?id=1234" />
Their code will pick up the id by using <?php echo $_GET["id"]; ?>
Thanks for your help.
You'll want to use .htaccess for this. Create a file called .htaccess and put it in the same folder mydomain.com is referencing. Put something like the following in there
RewriteEngine On
RewriteRule ^folder/(.*)$ folder?id=$1 [L]
Note: mod_rewrite must be enabled on apache, but it usually is.
Update: I've updated the .htaccess to reflect a local URL since you've made your desired functionality more clear. Using this .htaccess in conjunction with <iframe src="http://redirect_domain.com/folder/index.php?id=<?=$_GET['id']?>" /> should do the trick.
Create a .htaccess file and use something like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I feel the need to write my own answer down. The following two lines in your .htaccess should do exactly what you are asking for:
RewriteEngine On
RewriteRule ^folder/(.*)$ redirect_domain.com/folder/index.php?id=$1 [L]
According to the rewrite rule test site http://martinmelin.se/rewrite-rule-tester/ , when I use an input of folder/1234, it is redirected to redirect_domain.com/folder/index.php?id=1234 which is exactly what you asked for. There is no need for the <iframe... or any actual files in the /folder/ other than the .htaccess ...

PHP/.htaccess: removing php extension from url

I was using .htaccess code to remove .php extension for all my web pages. Here's the code I use:
RewriteEngine On
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
It doesn't seem to work. I think I'm missing something. When I type www.mysite.com/about/ to get www.mysite.com/about.php it returns error 404 (page not found). Can someone please shed some light.
Thanks,
Paul G.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# If folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# and file exist
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
# uncomment the below rule if you want the "/" to be required
# otherwise leave as is
# RewriteRule ^([^/]+)/$ $1.php [L]
# internally show the content of filename.php
RewriteRule ^([^/]+)/?$ $1.php [L]
The above rule will:
will not redirect if a folder exist
will not redirect if the file does not exist
will redirect what comes before the / if one is present as the file name
So it will work for all these examples:
http://domain.com/about/
http://domain.com/about
http://domain.com/contact/
http://domain.com/contact
If you want you can remove the ?, like the commented rule, to make it accept only URL's that end with a /.
http://domain.com/about/
http://domain.com/contact/
Now these are important step for the above to work:
It must go into the .htaccess on your root folder for example /home/youraccount/public_html/.htaccess
The Options before the rewrite rule are very important specially -MultiViews
The file must exist on the same place the .htaccess is for example in your case the about.php file
The PHP must be working obviously.
It seems like the slash at the end of your rule might be there, or it might not. Adding a ? makes it optional, so that mysite.com/about and mysite.com/about/ will both match.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-\s]+)/?$ /$1.php
It's hard to say if this is what's causing your problem, or if something else is, though. Does mysite.com/about.php also give you an error?

.htaccess file rewrite rule check

I want to rewrite my apache mod_rewrite pattern. I use to do with following:
RewriteEngine on
RewriteRule /^([^A-Z0-9a-z]+)\.html /search.php?search_id=$1&%{QUERY_STRING}
With the above rule I can't get what I expecting.
Can anyone tell what is wrong with my code and how to solve it.
Edited.
my requirement is simple.
I want to redirect if i enter /1.html to /search.php?search_id=1
Thats it.
This should work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !search\.php
RewriteCond %{REQUEST_URI} ^/([0-9]+)\.html [NC]
RewriteRule .* search.php?search_id=%1 [L,QSA]
Maps silently
http://example.com/FileID.html
To:
http://example.com/search.php?search_id=FileID
search.php is considered a fixed string, while FileID can be any number.
For permanent redirection, replace [L,QSA] with [R=301,L,QSA]
You have a typo in your rule try this:
RewriteEngine on
RewriteRule ^/([^A-Z0-9a-z]+)\.html /search.php?search_id=$1&%{QUERY_STRING}
By the way you could also try to use this:
RewriteRule ^/?([^A-Z0-9a-z]+)\.html /search.php?search_id=$1&%{QUERY_STRING}
This makes the leading slash optional.
Are you typing 1.html after the domain? That is if your domain is example.com, then you type example.com/1.html?
If it is you are doing then don't use / before the regular expression in .htaccess file.
Try adding the following into your main .htaccess file. That is located in the directory which has your home page of the website:
RewriteEngine On
#RewriteBase /
RewriteRule ^([A-Za-z0-9]+)\.html/?$ /search.php?search_id=$1 [NC, L]
I have used RewriteBase as a comment. If you use a shared server then keep it as a comment or delete the line.
EDIT: [NC, L] use to tell that, NC flag for rule no need to be case sensitive (no case) and L flag for stop if this was matched.

Using Mod Rewrite to rewrite a url?

I have the following format:
http://www.mydomain.com/view/product/ID/CAT/TITLE
ID and CAT are both numeric. So, some real examples:
http://www.mydomain.com/view/product/1/2/ipod
http://www.mydomain.com/view/product/3/4/40-inch-tv
etc
I want to know if I should try to use mod rewrite, and how, or if I should rather fix the PHP to handle the paths correctly. I want to map the above to something like:
http://www.mydomain.com/CATEGORY/SUB_CATEGORY/TITLE
Any ideas?
RewriteEngine on
RewriteBase /
RewriteRule ^([A-Za-z0-9]+)$ /$1/ [R]
RewriteRule ^([A-Za-z0-9]+)$ index.php?cat=$1
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$2/ [R]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php?cat=$1&subcat=$2
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$2/$3/ [R]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php?cat=$1&subcat=$2&title=$3
in php you can get the variables as you would normally do, also you will need a rule for all variables entered and/or only category or sub-category or you will end up with a 404 when someone tries to access those pages
I would say that your question is not totally clear to me. Going by your examples what do you want your target URL to become from http://www.mydomain.com/view/product/1/2/ipod.
Will it be: http://www.mydomain.com/2/1/ipod ?
If yes then enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^view/product/([0-9]+)/([0-9]+)/(.*)$ $2/$1/$3 [L,R,NC]

Mod Rewrite Slash Problem

I searched this problem in the site, but I couldn't find a solution.
I have a php page, call
http://www.domain.com/topic.php?name=xyz
I want to call this page with
http://www.domain.com/topic/xyz
What I tried is;
RewriteEngine On
RewriteRule ^topic/([^/]*)$ /topic.php?name=$1 [L]
However because of this "/" I get 404 error. If I try "-" instead of "/" it works. I guess "/" forward user to folder "/topic" so I need a solution to fix it.
The following should work given the fact that your topic name follows the pattern ([a-zA-Z0-9-_]+) - and if it doesn't match it, it probably should(and it's your job to sanitize it so that it matches), because it's a URL.
RewriteEngine On
RewriteRule ^topic/([a-zA-Z0-9-_]+).html$ topic.php?name=$1 [L]
NOTE:
In your request you say you want the url to look like this: http://www.domain.com/topic/xyz and in the .htaccess rule you tried to write it with .html at the end. If you don't want .html at the end you should do the following:
RewriteEngine On
RewriteRule ^topic/([a-zA-Z0-9-_]+)$ topic.php?name=$1 [L]
I found my solution to problem. It is a bit late though :)
I edit my .htaccess such that
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .* redirect.php [L]
I create a redirect.php as you see. In this file, I explode slashes, then by using if statement I set
$path = explode("/", $_SERVER['REQUEST_URI']);
switch ($path[1])
{
case 'topic': $_GET[name]=$path[2]; include('topic.php'); break;
}
As you see setting $_GET as a code isn't handsome, but it works like a charm! :) Thanks all for your helps.
Too bored to guess anymore.
My .htaccess (taken from your question):
RewriteEngine on
RewriteBase /
RewriteRule ^topic/([^/]*)$ /topic.php?name=$1 [L]
My topic.php:
<?php
var_dump($_GET);
Request:
http://localhost/topic/xyz
Result:
array(1) { ["name"]=> string(3) "xyz" }
So it just works on Apache 2.2.14
I think you need to espace the forward slash, like so:
RewriteRule ^topic\/([^\/]*)$ topic.php?name=$1 [L]
And in Bogdan's solution you're missing a + after the character class, now it only matches one character :P
If you have the line AddHandler type-map var in your httpd.conf, try commenting it out.
It might seem odd, but this functionality (which is not often used, but is enabled in the default httpd.conf for the Apache "It Worked!" page) has caused me grief along similar lines in the past.
It's something to do with the fact that you have a file "topic.php", and Apache allows you to call it like "topic/en-us". Or something like that. You'd have to look up the feature in the Apache docs to get the specifics; I'm just going off memory.

Categories