How to convert get strings to slug? - php

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.
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.
in view_sales.php I've also used a different header file and have get_header('sales');
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];
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
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.

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.

I have a website that is 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
Yes, its possible, e.g.:
RewriteEngine On
RewriteRule ^sales/(\d+)/userid/(\d+)/([a-z0-8]+)$ sales.php?id=$1&userid=$2&uid=$3 [L,NC]
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']
Yes, of course php will be able do that.

Related

Redirect old custom Posts URL to new WordPress URL

In short, I converted my old custom site to a new WordPress site, the domain remains same, I used PHP to insert thousands of old articles with its comments into WordPress database maintaining the same id sequence, meaning if the old links were:
www.mysite.com/index.php?id=11058
www.mysite.com/index.php?category=12
Than the new link are:
www.mysite.com/?p=11058
www.mysite.com/?cat=12
Everything was done well, the only problem is that I don't want to lose the old backlinks, and I want to use PHP to redirect, for example:
if (isset($_GET['old_id'])) { $id=$_GET['old_id']; $Wordpress_post_id = $id; }
How can I use this code in WordPress? and is this method better or redirect by .htaccess? Or is there another way that is better than the two?
I would do this in template_redirect:
This action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.
add_action(
'template_redirect',
static function() {
if(!isset($_GET['old_id'])){
return;
}
// Do custom look up here, possibly get_posts()
// Once you determine where to go, use wp_safe_redirect with the appropriate code (probably 301)
// https://developer.wordpress.org/reference/functions/wp_safe_redirect/
// Also possibly use get_permalink() to find the canonical link for the object
// https://developer.wordpress.org/reference/functions/get_permalink/
}
);
Unfortunately, the "custom stuff" is really dependent upon how you stored things. Is it post meta, did you manually insert post IDs, is it a custom lookup table?
If it is a true mapping of legacy IDs to PostIDs, you might even be able to use a plugin such as Redirection with a simple rule.
Since you mentioned (and tagged) .htaccess you could do it like this at the top of the .htaccess file (before the # BEGIN WordPress comment marker):
# Redirect "/index.php?id=<number>" to "/?p=<number>"
RewriteCond %{QUERY_STRING} ^id=(\d+)
RewriteRule ^index\.php$ /?p=%1 [R=301,L]
# Redirect "/index.php?category=<number>" to "/?cat=<number>"
RewriteCond %{QUERY_STRING} ^category=(\d+)
RewriteRule ^index\.php$ /?cat=%1 [R=301,L]
Where %1 is a backreference to the captured group in the preceding CondPattern in both cases. ie. the value of the id and category URL parameters respectively.
Test with 302 (temporary) redirects to avoid caching issues.
I recently had to do the same thing. I reorganized a site and moved the structure from the root (and all directory structure) to the blog folder. I experimented with several methods for WordPress, analyzed logs for issues, etc., and implemented the following method.
First I created a list of every page, article, etc.
Then I created a .htaccess file located in the site root directory.
In my example below I show the redirect for one page, but with two entries (trailing slash or not). The bottom portion handles files and directors, etc.
My .htaccess is about 600 lines long. I have not noticed any performance issues with the redirects.
Note: I am using a 302 for redirects, if yours are permanent, consider using a 301.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^aboutme$ https://www.example.com/blog/aboutme/ [L,R=302]
RewriteRule ^aboutme/$ https://www.example.com/blog/aboutme/ [L,R=302]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

htaccess redirect in wordpress

By using WordPress with Divi and custom php pages/scripts I ended with urls looking like this:
https://example.com/?day=today
https://example.com/?sport=1&league=123 (1=football and 123=premier_league)
I want to make them more user and SEO friendly and rewrite them in htaccess to:
https://example.com/?day=today ==> https://example.com/today
https://example.com/?sport=1&league=123 ==> https://example.com/football/premier_league
For the "day" link I tried this code, but it doesn't work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-z\-\_]+)?$ /?day=$1 [L,QSA,NC]
</IfModule>
For the other example with sport and league I wanted to find a solution in which I will manually add old link and new link, tried redirect 301, but I think it can't work, right?
Redirect 301 https://example.com/?sport=1&league=123 https://example.com/football/premier_league
I also tried Redirection plugin in WP, but it created Rule which destroys page and makes error 500:
RewriteRule ?sport=1&liga=123 https://example.com/football/premier_league [R=301,L]
or
RewriteCond %{QUERY_STRING} ^sport=1&league=123$
RewriteRule ^$ https://example.com/football/premier_league [R=301,L]
which gaved address: https://example.com/premier_league/?sport=1&league=123
I was also wondering if I can make it with pure php, since in PHP I can easilly get proper strings for id's of the sports and leagues. Unfortunantelly changing "?sport=1&league=32758" into "?sport=football&league=premier_league" and then clean it with rewrite in htacces is not possible right now.
I'm not expecting whole code for both problems, I believe that some directions would be enough. I browse stack and google and coudn't find any examples with links that looks like mine, so example.com/?sport=1 not example.com/page?sport=1
After further investigation I believe the problem is in the way I'm opening my custom pages. I'm using shortcodes to initiate a script and inside the script my php code is included with parameters. I think that because of that I got to this page https://example.com/premier_league/?sport=1&league=123 which was pretty close, but not working.
Now my question is how to send those parameters to proper script but not into url.

Htaccess rewrite rule php link not working Wordpress

I've made some search about .htaccess Rewrite function, but I couldn't find any example with an html reference.
I'm trying to redirect from my example.com/products/category/ link to another page.
Here's the link what I'd like the .htaccess file to redirect:
<a href="example.com/products/category/?category=bathroom">
I'd like to achieve this style:
example.com/products/category/bathroom/
Here's my .htaccess ReWriteRule:
RewriteRule ^products/([A-Za-z0-9-]+)/?$ /products/category.php/?category=$1 [L] # Process category
Note that I'm using two different php pages for both. page-category.php and category.php
I've places it after
RewriteEngine On
RewriteBase /
but still it doesn't work.. The only time something happened when I tried some combinations, it threw back an internal server error. Is there a problem with my RegEx or am I not doing the linking right ?
EDIT: I'm using wordpress, I forgot to mention that, sorry.
You need to use Redirect 301
RedirectMatch 301 ^example.com/products/category/?category= /example/home/page-category.php
Or you can use mod_rewrite instead:
RewriteEngine On
RewriteRule ^example/products/category/?category= /example/home/page-category.php [L,R=301]
same you can do for another file.
as i understood from your question you need something like this in category directory .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) category.php?category=$1 [QSA]
Now anything that is not a directory or a file inside category directory will be rewriten like /category/bathroom will be rewriten to (without redirection) /category.php?category=bathroom

How can I make pretty URL link with wordpress?

I've got some questions about making pretty URL link.
On my website home page, I have a drop down list of text (city and state) which is going to be my parameter. I'm sending the value through GET METHOD to a WordPress template page which system will display contents based on passed value. The display content part is working successfully, however, I have some question about link.
Right now, the link of the page is showing
www.mydomain.com/list/?state=NY&city=Newyork or
www.mydomain.com/list/?state=IL&city=Chicago
I prefer the link to be the following pretty format, ...
www.mydomain/list/NY/Newyork
www.mydomain/list/IL/Chicago
I have researched on many sites and found recommendation on using htaccess. I'm using the following code but still link doesn't get changed to the pretty format.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([0-9]+)/?$ ?district=$1
RewriteRule ([0-9]+)/([^/]+)/?$ ?district=$1&templename=$2 [L]
</IfModule>
# END WordPress
What you're looking for should be done using wordpress' add_rewrite_rule function. You can read more about it here
Here's a sample that may be what you're looking for:
<?php
add_rewrite_rule('list/([0-9|a-z|A-Z]*)?/?([0-9|a-z|A-Z]*)?/?([0-9|a-z|A-Z]*)?/?([0-9|a-z|A-Z]*)?','index.php?$matches[1]=$matches[2]&$matches[3]=$matches[4]','top');
?>
To explain things in detail:
First, we declare a regular expression to match any get parameters after the "list" slug. (i.e: www.mydomain.com/list/)
Secondly, we declare how wordpress should interpret it. The file will always be index.php for wordpress. The parameters will line up like www.mydomain.com/list/state/IL/city/Chicago which will translate to www.mydomain.com/list/?state=IL&city=Chicago in your code for you to use your $_GET parameters
top tells Wordpress that these rules have precedence over wordpress' own rules.
You may need to call flush_rewrite_rules( false ) or $wp_rewrite->flush_rules() to flush the rules and have your changes come into effect

.htaccess dynamic url (old site) to static url in magento

Long time reader first time poster.
I have read through what feels like 100's on similar posts trying to make it fit what i am trying to do.
Apologies if its been covered. I believe I have the correct code but it maybe conflicting with something else?
I have old URLs from an old ASP cart and want to 301 redirect to new magneto static pages.
There are only 5 of these URLS so don't need a bulk action.
example:
OLD URL: http://test.com.au/shopcontent.asp?type=FAQ%20and%20Terms
NEW URL: http://test.com.au/privacy-policy-cookie-restriction-mode
I know that from reading other posts you can not simply use:
Redirect 301 /shopcontent.asp?type=FAQ%20and%20Terms /privacy-policy-cookie-restriction-mode
So I have worked out that this should work:
RewriteCond %{QUERY_STRING} ^type=FAQ%20and%20Terms$ [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]
I have also tried removing the %20 for the spaces like this:
RewriteCond %{QUERY_STRING} ^type=FAQ\ and\ Terms$ [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]
When I try and access the old URL it just goes to the 404 page. I am using standard .htaccess file that came with Magento (I can post if that helps);
I am using a VPS which mod rewrite is enabled. I have other Redirect 301's for non dynamic URLS.
Are there any logs that I can look at?
Appreciate any help or advice.
Thanks Rudi
ps. I found this post which got me to where i am now
RewriteCond %{QUERY_STRING} in mod_rewrite (dynamic to static URL) not working
You cant use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} "^type=FAQ\%20and\%20Terms$" [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]

Categories