Jump to content
Sign in to follow this  
azumica

.htaccess and PHP

Recommended Posts

Hi how can I make this URL clean

http://localhost/?Active=View&PostCategory=1

i want to remove the ?,&, = make them into /

this is the php code that manipulate the link

<?php
Class ControlURL{


    public static $_page = "Active";
    public static $_folder = Web_Body;
    public static $_params = array();


    public static function cPage() {
        if(isset($_GET[self::$_page])){
            return $_GET[self::$_page];
        }else{
            return 'index';
        }
    }


    public static function getPage() {
        $page = self::$_folder.DS.self::cPage().".php";
        $error = self::$_folder.DS."error.php";
        if(is_file($page)){
            return $page;
        }else{
            return $error;
        }
    }


}

 

Share this post


Link to post
Share on other sites
Posted (edited)

Since those are arguments for the request, I don't understand why you would want to do that. Please explain what you are trying to accomplish. A URL that looks like http://localhost//Active=View/PostCategory=1 makes no sense. If you rewrite it that way with .htaccess, what/how do you expect it to be processed?

Edited by gw1500se

Share this post


Link to post
Share on other sites

Fix the link rather than try to translate it. It still makes no sense to me.

Share this post


Link to post
Share on other sites

i have this RewriteRule

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*/|)([^/]*)$ index.php?Active=$2[L]
 

but it only shows

http://localhost/View

 

i dont know to get the pass variable and its value

 

which is the

&PostCategory=1

Share this post


Link to post
Share on other sites
Posted (edited)

You need to explain what you are trying to accomplish. I can not imagine why you want to do this with .htaccess. Perhaps we can suggest a better way.

Edited by gw1500se

Share this post


Link to post
Share on other sites

I want a clean url. Thanks man i thought clean urls can be done with .htaccess

Share this post


Link to post
Share on other sites

I think you mean "pretty URL" or sometimes "friendly URL".

Yes, you can do that with your .htaccess file, but it'll probably take some php coding as well. If you look at open-source CMSes and frameworks you'll get the basic idea - WordPress, Laravel, and Codeigniter are all examples I've personally used. Check out the .htaccess and index.php files, and follow the trail from there. You can also just google it and come up with plenty of examples.

  • Thanks 1

Share this post


Link to post
Share on other sites

mod_rewrite will allow you to take a "pretty url" and parse it into the the parameters your site understands.  It doesn't automagically translate a url with parameters into a "pretty" url.  You need to do that yourself.  Hopefully you understand this.

8 hours ago, azumica said:

RewriteRule ^(.*/|)([^/]*)$ index.php?Active=$2[L]

You have to understand regular expressions and capturing groups.

  • Thanks 1

Share this post


Link to post
Share on other sites

I guys i figured out my .htaccess is working now but i dont know how to change the redirect in <a href="">

 

this is my code

	echo "<li><a href=\"/?Active=View&amp;PostCategory=".$cat['CategoryID']."\"";
      
 

How can i redirect this in <a href=

 

with this

 

http://localhost/View/1

Share this post


Link to post
Share on other sites

is this okay?

 

"<li><a href=\"/View/".$cat['CategoryID']."\"";

Share this post


Link to post
Share on other sites

Whatever lines up with your mod_rewrite rules.  You should either interpolate your variable or not.  You have a mix of the two  (concatenation and interpolation).

 

# Interpolation
"<li><a href=\"/view/{$cat['CategoryID']}\">$somevar</a></li>";

#concatenation (use single quotes!)
'<li><a href="/view/' . $cat['CategoryID'] . '">' . $somevar . '</a></li>';

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.