Viewed   90 times

I have a index.php which handle all the routing index.php?page=controller (simplified) just to split up the logic with the view.

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([wd~%.:_-]+)$ index.php?page=$1 [NC]

Which basically: http://localhost/index.php?page=controller To

http://localhost/controller/

Can anyone help me add the Rewrite for

http://localhost/controller/param/value/param/value (And soforth)

That would be:

http://localhost/controller/?param=value&param=value

I can't get it to work with the Rewriterule.

A controller could look like this:

    <?php
if (isset($_GET['action'])) {
 if ($_GET['action'] == 'delete') {
do_Delete_stuff_here();
}
}
?>

And also:

    <?php
if (isset($_GET['action']) && isset($_GET['x'])) {
 if ($_GET['action'] == 'delete') {
do_Delete_stuff_here();
}
}
?>

 Answers

3

Basically what people try to say is, you can make a rewrite rule like so:

RewriteRule ^(.*)$ index.php?params=$1 [NC, QSA]

This will make your actual php file like so:

index.php?params=param/value/param/value

And your actual URL would be like so:

http://url.com/params/param/value/param/value

And in your PHP file you could access your params by exploding this like so:

<?php

$params = explode( "/", $_GET['params'] );
for($i = 0; $i < count($params); $i+=2) {

  echo $params[$i] ." has value: ". $params[$i+1] ."<br />";

}

?>
Sunday, August 14, 2022
2

you can get the variable values automaticly by this method

RewriteEngine On
RewriteBase / 
RewriteEngine On Options All -Indexes RewriteBase /directoryname/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    ###############  SEO     ##########################

#
http://www.example.com/hello/booboo/ it takes the url  after .com/
    RewriteRule ^(.*)$ getme.php?url=$1 [QSA,L]

for example if i enter http://www.example.com/hello/bobo/

it is going to replace and take "/hello/bobo/" part now you can use this in .htacces file Also if you want to redirect to another page and filter the variable value you should modify $1 because all data in this.

edit: in that example i get url after my domain and i redirect to get.php also you can divide the url using split method by "/" Let's see the get.php page to understand this method

getme.php:

<?php

//we redirect to get in url=$1 so our get method name is url
$parca = explode("/", $_GET["url"]); //and we divided by slash the url.

echo $parca[0];//this is first part "/hello/
echo $parca[1];// and this is second part "/booboo/;
?>
Friday, September 23, 2022
 
tony_m
 
1

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 ^(profile)/(theuser)/?$ $1?user=$2 [L,QSA,NC]
Friday, September 23, 2022
1

EDIT

I completely setup your files on my machine

//  /.htaccess

RewriteEngine on

RewriteRule ^(public)($|/) - [L,NC]

RewriteCond $1 !^(index.php|robots.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

.htaccess in the public folder:

//  /public/.htaccess

Options -Indexes
RewriteEngine off

This disables rewriting like you wanted.

/public/                 -> 403 Forbidden
/public/favicon.ico      -> 200 File found
/public/not-existing.ext -> 404 File not found

Do you have a index.php in you public folder? Maybe you could remove that one..

What kind of machine your testing on?

I tested it on Linux + Apache 2 + PHP5.3

I can give you more support in the afternoon (my time +2 GMT)

EDIT 2

When I remove this line from /.htaccess is still works

RewriteRule ^(public)($|/) - [L,NC]

Everything is handled by the .htaccess in the public folder.

Maybe it's a caching problem in your browser. Try a different browser/clean up history/install app to remove cache.. (depending on what browser you're using)

Thursday, October 13, 2022
 
shardul
 
4

My best bet would be to simply add get variables to the regex, like so:

RewriteRule ^account/blogs/([0-9]+)/([^s?]+)/?(?(.*))?$ /account/blog.php?blogId=$1&$4 [L,QSA]

This would rewrite

/account/blogs/1/ThisIsWhereTheTitleGoes?delete=1

to

/account/blog.php?blogId=1&delete=1

It would also support additional variables.

Friday, November 18, 2022
 
dave_d
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :