I feel like I've tried everything to get this to work, but my currently-limited knowledge of wordpress may be responsible.
I want to modify part of a query string in a url, so
/my_site/wp-admin/admin.php?page=events
will redirect to:
/my_site/wp-admin/admin.php?page=espresso_events
These urls are hard-coded within the plugin, but I want to be able to define my own query string that will point to the url, adding any additional query parameters onto the end.
function plugin_activate()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function remove_espresso_url()
{
add_rewrite_rule('page=events&(.*)$',
'wp-admin/admin.php?page=espresso_events&$matches[1]',
'top'
);
}
register_activation_hook(__FILE__, 'plugin_activate');
add_action('init', 'remove_espresso_url');
The rule is appearing in $wp_rewrite->non_wp_rules. I've tried various rule variations, and even entirely different rules, but nothing works. I've checked my local server settings and url rewriting is enabled, and my .htaccess file is fine as well.
I've also installed the Monkeyman Rewrite Analyzer plugin, and my rewrite rules aren't appearing in there at all.
Here's the resulting value from wp_rewrite->mod_rewrite_rules()
RewriteEngine On
RewriteBase /event_espresso/
RewriteRule ^index\.php$ - [L]
RewriteRule ^page=events&(.*)$ /event_espresso/wp-admin/admin.php?page=espresso_events&$matches[1] [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /event_espresso/index.php [L]
Edit: This appears to be because I'm trying to rewrite the query string, however what I need to do is use RewriteCond %{QUERY_STRING}, but I'm not sure Wordpress allows me to add new rewrite conditions.
I finally resolved the problem.
For the sake of brevity, I'll just link to my new question, where I've posted the answer; Custom .htaccess rewrite rules in wordpress not executing
Related
I know this can be done using Rewrite engine but I am unable to do this
This is my .htaccess file
# 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]
</IfModule>
# END WordPress
I have this url www.mysite.com/report?page=my-report-name
Now what I want to acheive is this : www.mysite.com/report/my-report-name
The file where I am accessing this get variable name page is reports.php and its not in my root directory
Path to my file is : root/themes/fount/intel/reports.php
Can anyone help?
Try this code,
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# /anything/anything -> anything.php?url=anything
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9_])/([^/]*)$ /$1.php?url=$2 [L]
</IfModule>
If the page.php filename will always be the same, then do it like this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# /page/anything -> page.php?url=anything
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ /page.php?url=$1 [L]
</IfModule>
Thanks!
Updated answer
To add a routing which points to an external handler PHP file, the following snippet should be used:
function wprre_add_rewrite_rules() {
global $wp_rewrite;
// pattern with regexps
$wp_rewrite->add_external_rule( '^wp_report/([\w\d-]+)/?', PATH_TO_THE_EXTERNAL_HANDLER.'report.php?report_name=$1' );
}
add_action('init', 'wprre_add_rewrite_rules');
You can spot one difference in the parameter handling between add_external_rule() and the add_rewrite_rule. You must use the match selector as the Apache uses it in this case.
This snippet must placed in a file which is always loaded by your plugin or theme. If you write a plugin it can be the main plugin file. In case of theme development it can be the main functions.php file.
The custom GET parameter registration is working as it was mentioned in the Original answer.
IMPORTANT
After you edited the rewrite rules via code (external or internal both) you must go to the Permalink settings page in the admin panel and click to the Save button without any changes. This is necessary, because this will flush the rewrite rules and the WP will write into the .htaccess file the rules.
This is the reason why I recommend you to hook on the plugin activation event and register the rewrite rules then and immediately run a flush_rewrite_rules() command.
NOTES
The problem with the original answer was that, the add_rewrite_rule() function only works if you route to the default basic index.php. You can only modify the parameters, but you can not route to an external file.
Original answer
I think you should use the WordPress API to achieve this. You will need to add a rewrite rule and tag in you theme or plugin with this syntax:
!! Disclaimer this is only working to route to the basic index.php !!
For the routing, add a rewrite rule which points to your PHP file.
function custom_rewrite_basic() {
add_rewrite_rule('^report/([\w-]+)/?', 'index.php?page=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');
If you want to use a query parameter which is not in the standard WP parameter list, you need to add that custom parameter name.
function custom_rewrite_tag() {
add_rewrite_tag('%page%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
Be aware to you use built in parameters if you do not use as the WP API.
In this Codex article you find more details about the topic:
https://codex.wordpress.org/Rewrite_API/add_rewrite_rule
For troubleshooting and deeper dive understanding you might to check this Codex article too, which describes the proper rewrite rule usage. Because some in circumstances you need to reset the rewrite rules (plugin activation / deactivation).
https://codex.wordpress.org/Function_Reference/flush_rewrite_rules
Finally this solution worked for me
I have added this in my functions.php file that is in our theme folder
function rewrite_photo_url(){
add_rewrite_rule('^report/([^/]*)/?','index.php?page_id=2671&value=$matches[1]','top');
}
function register_custom_query_vars($query_vars){
$query_vars[] = 'value';
return $query_vars;
}
add_action('init','rewrite_photo_url');
add_filter('query_vars','register_custom_query_vars');
and then in my php file I use get_query_var('vale') to get my parameter value
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.
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
I'm trying to convert a query string;
http://atwd/books/course?course_id=CC100&format=XML&submit=Submit
Into a segment URI;
http://atwd/books/course/CC100/XML
I'm working in CodeIgniter.
I was looking at a stackoverflow answer that said to check CodeIgniter's URL segment guide, but I don't think there's any information on how to convert a query string into a segment URI. There is, however a way to convert a segment URI into a query string, which is bringing up a load of results from Google too.
Following another stackoverflow answer, I tried this in my .htaccess file but nothing seemed to work
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^$ /course/%1/format/%2 [R,L]
In my entire .htaccess file I have this;
<IfModule mod_rewrite.c>
RewriteEngine on
#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]
#Source: https://stackoverflow.com/questions/3420204/htaccess-get-url-to-uri-segments
#Format Course function requests
RewriteBase /
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^$ /course/%1/format/%2 [R,L]
</IfModule>
This is in my root directory of Codeigniter screenshot
My code in the .htaccess file isn't working, I refresh the page and nothing happens. The code to hide the index.php is working though. Does anyone know why?
The notion of "converting URLs" from one thing to another is completely ambiguous, see the top part of this answer for an explanation of what happens to URLs when redirecting or rewriting: https://stackoverflow.com/a/11711948/851273
There's 2 things that happen, and I'm going to take a wild stab and guess that you want the 2nd thing, since you're complaining that refreshing the page doesn't do anything.
When you type http://atwd/books/course?course_id=CC100&format=XML&submit=Submit into your browser, this is the request URI that gets sent through mod_rewrite: /books/course. In your rule, you are matching against a blank URI: RewriteRule ^$ /course/%1/format/%2 [R,L]. That's the first reason your rule doesn't work. The second reason why it doesn't work is because above that, everything except images and index.php and robots.txt is being routed through index.php. So even if you were matching against the right URI, it gets routed before your rule even gets to do anything.
You need to correct the pattern in your rule to match the URI that you expect to redirect, and you need to place this rule before the routing rule that you have. So everything should look roughly like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^/?books/course$ /course/%1/format/%2 [R,L]
#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]
</IfModule>
You'll need to tweak the paths to make sure they match what you are actually looking for.
To both redirect the browser and internally rewrite back to your original URL, you need to do something different.
First, you need to make sure all of your links look like this: /course/CC100/format/XML. Change your CMS or static HTML so all the links show up that way.
Then, you need to change the rules around (all before your codeigniter routing rule) to be something liek this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# redirect browser to a URI without the query string
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /books/course/?\?course_id=([^&]+)&format=([^&]+)
RewriteRule ^/?books/course$ /course/%2/format/%3? [R,L]
# internally rewrite query string-less request back to one with query strings
RewriteRule ^/?course/([^/]+)/format/([^/]+)$ /books/course?course_id=$1&format=$2&submit=Submit [L]
#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]
</IfModule>
I'm not going to address the misunderstanding already addressed pretty well in the other answer and comments, and I can't speak for CodeIgniter specifically, but having given their URL routing docs a quick skim, it seems pretty similar to most web frameworks:
You probably just want to direct all traffic (that doesn't match physical files) to the frontend web controller (index.php) and handle the URL management in CodeIgniter's routing, not a htaccess file.
To do that, your htaccess could be as simple as:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [QSA,L]
</IfModule>
This, as I said, will redirect any traffic that doesn't match an physical file such as robots.txt or an image to your index.php.
Then, using the routing as described in the docs (http://ellislab.com/codeigniter/user-guide/general/routing.html) you can take in parameters and pass them to your controllers as you see fit, there is no need to 'convert' or 'map' anything, your URL's don't need to resolve to /?yada=yada internally, based on your routing rules CodeIgniter can work it out.
You'll need wildcard routes such as this from the docs:
$route['product/:id'] = "catalog/product_lookup";
A rough example of what yours might end up looking like would be something like:
$route['course/:id/format/:format'] = "course/something_or_other_action";
If I'm understanding you correctly, you might be over-thinking it. I have something similar in my own code.
I have a controller named Source. In that controller, I have the following method:
public function edit($source_id, $year)
{
# Code relevant to this method here
}
This produces: http://localhost/source/edit/12/2013, where 12 refers to $source_id and 2013 refers to $year. Each parameter that you add is automatically translated into its own URI segment. It required no .htaccess trickery or custom routes either.
My permalink structure is set so I have url.com/page
I made a basic PHP script prior to installing wordpress that uses $_POST data to display the correct set of information, so the base would look like url.com/work.php?featured=print
After adding this to my Wordpress installation with the rewrite from the permalink structure above, the link actually works as:
url.com/work/?featured=print
I'm having trouble getting the extra rewrite to work so that a clean url.com/work/print will work properly.
This is what my .htaccess file looks like, I appended the last line before the end IfModule tag hoping that would take anything work/[page]/ and direct it to work/?featured=[page]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
RewriteRule ^work/([^/.]+)/?$ work/?featured=$1 [L]
</IfModule>
# END WordPress
any help?
EDIT
Discovered rewrite via the functions page and found this quite resourceful
Rewrite rules for WordPress
however, my rule is taking .com/work/print (work/?feat=print) and simply showing it as .com/work without the page data being passed through
'work/([^/]+)/?$' => 'work&feat=$matches[1]'
The above is the only thing I changed from post I just referenced. I tried keeping is specific to get it to work first to avoid any loose ends...
Still not working properly though
The wp-admin dashboard has a tool that helps with the rewrites. When I ran a few WP sites, I used it to achieve what I wanted. Did you try that first?
http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/
For some reason, couldn't get anything to work, but ^ is quite simple and worked great