Mod Rewrite Slash Problem - php

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.

Related

.htaccess URL rewrite ignored

.htaccess newbie here.
I have a URL like this:
example.com/lesson-plans/earth-sciences/show_lesson_plan.php?title=Some_Title&id=6
that I need to be rewritten like this:
example.com/lesson-plans/earth-sciences/some-title-6
I am using the following .htaccess URL rewrite:
RewriteEngine On
RewriteRule ^lesson-plans/earth-sciences/([^/]*)/([^/]*)$ /lesson-plans/earth-sciences/show_lesson_plan.php?title=$1&id=$2&cat=3 [L]
However, when I hover over/click on links of the original format (example.com/lesson-plans/earth-sciences/show_lesson_plan.php?title=Some_Title&id=6), they are not being rewritten. I've tried a few different rewrite rules, but nothing works.
How can I make this rewrite work? As far as I know, .htaccess is working on my server and rewrites are permitted.
You were close
RewriteEngine on
RewriteCond %{QUERY_STRING} title=([^&]+)&id=([^&]+)
RewriteRule ^lesson-plans/earth-sciences/show_lesson_plan.php$ /lesson-plans/earth-sciences/%1-%2 [QSA,L,R]
Randomly while lying in bed last night.
You have the rewrite rule back to front. you have to add the rule for the rewritten url to turn it back into an ugly one
see: http://martinmelin.se/rewrite-rule-tester/
RewriteEngine On
RewriteRule ^lesson-plans/earth-sciences/(.*)-(.+)$ /lesson-plans/earth-sciences/show_lesson_plan.php?title=$1&id=$2&cat=3 [L]
so
lesson-plans/earth-sciences/some-title-6
becomes
/lesson-plans/earth-sciences/show_lesson_plan.php?title=some-title&id=6&cat=3
I ended up using the following code and am posting it as an answer in the event someone else finds this useful:
RewriteEngine on
RewriteRule ^([^/.]+)/([^/.]+)$
show_lesson_plan.php?title=$1&id=$2
This will take a url (like this: http://example.com/lesson-plans/earth-sciences/a-cloud-in-a-bottle-i/8923) and run it through this: http://example.com/lesson-plans/show_lesson_plan.php?title=$1&id=$2
Note that I changed the original URL slightly, opting to break the id out of the title string.

Pass a clean parameter through URL

I'm using localhost, and in my index.php page I have this code:
<? echo 'LANG IS '.$_GET['lang']; ?>
When I type localhost on the URL it only shows LANG IS, obviously, but if I type localhost/en I see a 404 Not Found message. I have to type localhost?lang=en to show my index.php code. I want to type localhost/en instead of localhost?lang=en and get the same result.
I'm using Apache2 and I have mod_rewrite enabled. I also have a .htaccess file with this code (I have changed and tested it a lot of times):
RewriteEngine on
RewriteRule ^/([a-zA-Z0-9]+|)/?$ index.php?lang=$1 [L,QSA]
I have been reading about .htaccess and clean urls for days but I couldn't make this work. Any ideas? Thank you so much in advance.
Most likely your .htaccess isn't even enabled. Verify it first
To check if your .htaccess is enabled try putting same random/garbage text on top of your .htaccess and see if it generates 500 (internal server) error or not?
It it is not enabled then then you will need AllowOverride All line in <Directory "/var/www/>` section.
Once it is enabled following rule should work for you:
RewriteEngine on
RewriteRule ^(\w+)/?$ index.php?lang=$1 [L,QSA]
Try getting rid of the leading slash in the pattern:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+|)/?$ index.php?lang=$1 [L,QSA]
URI's that are sent through rules in the htaccess files have the leading slash stripped off so the pattern needs to omit it.
The problem is probably in the regular expression.
Try with this one:
RewriteEngine on
RewriteRule ^/([a-zA-Z0-9]+)/ /index.php?lang=$1 [L,QSA]

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 ...

mod_rewrite freakishly appends 21 slashes to URL

I have what I thought was a simple mod_rewrite rule.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/([a-z0-9_\-]+)/?([a-z0-9_\-]+)?(.*)?$ store/$2?store_name=$1$3 [L]
However, when I go to a URL like:
http://www.mydomain.com/store/e95_airport/
It turns into:
http://www.mydomain.com/store/e95_airport/////////////////////
I'm expecting the URL to load:
index.php?store_name=e95_airport
It works fine when I call a URL like:
store/e95_airport/some-page-other-than-index.php
I've disabled all other mod_rewrite rules just in case there was a conflict somewhere else in my .htaccess file. Is there anyone here who can diagnose what's wrong with my rewrite rule? Any help is greatly appreciated!
The problem is that the site is getting itself into a loop because you're redirecting it to essentially the same address.
Okay, so you are changing it with the rewrite, but the URL it rewrites it to still matches the expression.
The way mod_rewrite works is that once it's rewritten the URL, it then effectively requeries itself with the new URL. So if your new URL still matches the expression, it will get into a loop.
Why it stops at 21 slashes? This is because Apache will detect when it's in a loop, and stop if it requeries itself twenty times.
As #Spudley said, you are into a loop.
Looking to your rewrite rule, and looking the result you want, I would say that the rewrite rule is wrong also. It should be something like this:
RewriteRule ^store/([a-z0-9_\-]+)/?([a-z0-9_\-]+)?(.*)?$ index.php?store_name=$1 [L]

Htaccess mod_rewrite problem

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.

Categories