Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert get strings to slug?

Bounty Note: I've found my solution for my question using add_rewrite_rules. I will reward the bounty to anyone who can provide me with the same solution in apache RewriteRules format.

I've read How can I create friendly URLs with .htaccess? but it still difficult and complicate to me.

I am running WordPress Multisite, and I have a sub domain website cp.example.com and I'm writing a php app on this sub domain.

I have a website address that is looks like this:

http://cp.example.com/sales.php?id=12345&userid=123456&uid=83hkuhdqhuhd873xsuhdqwiuhdiq

Is it possible for me to let user access the website via:

http://cp.example.com/sales/12345/userid/123456/83hkuhdqhuhd873xsuhdqwiuhdiq

And if I were to do that will php still be able to do $_GET on the values?

e.g $_GET['id'], $_GET['userid'] and $_GET['uid']?

I'm using WordPress for my base, but i'm writing the app end, using a different table.

I'm trying to avoid using custom post type just to achieve the above.

I've tried the following.

  1. Create a template file view_sales.php in view_sales.php I will require $_GET['id'], $_GET['userid'] and $_GET['uid'] in order to retrieve info from mysql.

  2. in view_sales.php I've also used a different header file and have get_header('sales');

  3. in header-sales.php I've added the following code gotten from the above stack overflow page.

    $path_components = explode('/', $_GET['url']);
    $id=$path_components[0];
    $userid=$path_components[1];
    $uid=$path_components[2];
    
  4. I created a new wp page with slug sales so now the website is http://cp.example.com/sales/?id=123456&userid=123456&token=98917397219372iheu1i

  5. I only have one .htacess website in my /public_html/example.com domain since it's a multisite so I added the code suggested above to my .htaccess and now it looks like this.

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
    RewriteRule . index.php [L]
    
    <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    </IfModule>  
    

Nothing is working at the moment, still redirecting to the ugly url.

Edited:

I've tried the following

RewriteCond %{HTTP_HOST} !^cp\.example\.com [NC]
RewriteRule ^listing/([a-z0-9]+)$ listing/?action=$1 [L,NC] 
RewriteRule ^view/([a-z9-9]+)$ view/?id=$ [L,NC]

I tried different variants of all the above but nothing works.

Edited for add_rewrite_rule method which i tried

function pa_custom_rules() {
    add_rewrite_rule('^/listing/([^/]+)/$', 'index.php?pagename=listing&action=$matches[1]', 'top');
}
add_action('init', 'pa_custom_rules');

Printed the rewrite array and I can see the new rule

      [^/listing/([^/]+)/$] => index.php?pagename=listing&action=$matches[1]

Visiting index.php works but going to /listing/test/ fails. It returns 404 error.

like image 913
Someone Special Avatar asked Nov 09 '22 15:11

Someone Special


1 Answers

Finally solved problem which bothered me for last few days.

It appears Rewrite rules for specific domains does not work.

If you wish to change

http://subdomain.example.com/listing?action=add(e.g. or DEL or VIEW)

of a Wordpress MULTISITE to

http://subdomain.example.com/add/ (e.g.or DEL or VIEW)

you have to not only add rewrite rules, but it's also compulsory to rewrite/add the tag to query_var.

You need to add the following to function or create a plugin to initiate it once.

function pa_custom_rules() {
    add_rewrite_rule('listing/(.*)$', 'index.php?pagename=listing&action=$matches[1]', 'top');
   add_rewrite_tag('%action%', '(.*)');
}
add_action('init', 'pa_custom_rules');

once done you will be able to access http://subdomain.example.com/listing/add

In your page (in this case, my page name is 'listing'), you will no longer get 404 error and you can get the value of "action" (in this case Add/Del/View) by using

    http://subdomain.example.com/listing/**add**
    $a = get_query_var('action');
    echo $a; //prints **add**

Note: You cannot get the value of action using $_GET like how RewriteRule works, but this should be able to get most things done.

I can keep working on wordpress and get all my customise beautiful links and throw away my last 3 days of work exploring Laravel and Symfony3. Thank God.

like image 121
Someone Special Avatar answered Nov 15 '22 05:11

Someone Special