Using $_POST data after wordpress url rewrite - php

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

Related

Wordpress - Sub domain URL slash / missing after domain and before Post & page slug

WordPress page/page is not working after moved the file to subdomain. URL structure domain.com/subdomain/post1 is loading as domain.com/subdomainpost1 - slash (/) is missing before post/page slug. Home page and Other site assets working fine (i.e., images, css, js) - Only post and pages not loading.
Site .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /subdomain/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subdomain/index.php [L]
</IfModule>
# END WordPress
I have updated different permalink and still, it's not working. Also, deactivate all plugins and tested, still no fix. Someone, please help me to fix.
Generally you shouldn't be messing around with .htaccess files, if you don't fully understand the rewrite rules you're changing or the security risks.
Besides that, I assume that your "home" and "siteurl" are both updated in the database, so the easiest way to go from here, would probably be to delete the .htaccess file and then save permalinks in the admin page.
By doing so, WordPress will automatically generate a new .htaccess file that logically should be working out of the box. Then you can add your authorization block afterwards. That should go somewhere below the # END WordPress comment. You should never have to change the default WordPress .htaccess rules as it potentially breaks stuff.
If it still messes up, try searching your database for wrong formatted url's etc.

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.

Wordpress - permission denied

I have a website and I'm passing it to another server. I using permalink day and name. When I try to access to any page I receive this message:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
If I change the permalink to plain it works but I need to set it in day and name
I don't know what kind of problem is that.
How can I solve that?
Take a look at chmod in wordpress files/dirs
https://codex.wordpress.org/Changing_File_Permissions
OTher thing may me htaccess.
https://codex.wordpress.org/htaccess
As My Experience with WordPress I Advice First Set Permalink again default one ... and check is everything working ? Also try in build other permalink options is that working?
If above both working then write your custom permalink ..
If above booth not working then Check Rewrite option of domain it must Allow .htaccess to fix just replace .htaccess file https://codex.wordpress.org/htaccess
# 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
This simple tip will fix your issue ... DONT FORGET TO CLEAR CACHES
Cheers

.htaccess redirect from Wordpress URL to Normal site URL

I have rebuild a site. The old site was WordPress and new site is normal php site with same amount of pages.
I do not want to lose the outside plus google links to this pages and need to do a redirect in .htaccess
I know how to do this with normal links e.g. www.domain1.com/guesthouse.html to www.domain2.com/guesthouse.php
I do not understand the wordpress link with no ".php or .html" at the end of the link.
The old link is www.doamin.com/guesthouse/ (only this without .html or .php)
New link must be www.domain.com/gusethouse.php
The WordPress .htaccess file looks like this
`# 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`
Can I delete the above code and replace with
`Redirect 301 /guesthouse/ http://www.domain.com/guesthouse.php`
or must I add it to the existing .htaccess code.
Do not know how to do it right, please help.
Thank you.
The .htaccess file will execute prior to your PHP interpreting engine running. This means that you're higher in the execution chain than the Wordpress software.
If you're not going to use your Wordpress website anymore, then there's no reason to send requests to it!
Instead, just simply remove all of that code, and rewrite the URL's accordingly.

How to redirect all API requests using .htaccess, while keeping asset requests intact?

TL; DR: I would like to hit the index-api.php file if api is found in the URL, but then simply keep all other requests pointing to the site/dist directory as if it were the 'root' of the site.
So, I've spent way too many hours on this and trust me, I've dug through all of the resources for mod_rewrite. I guess I'm just not quite understanding and figured I'd ask on here.
What I want to do, in theory, seems simple. I'm building a single page application (Angular App) using Grunt, outputting that to a the root of a WordPress install. The WordPress install is simply serving up an API using the WordPress JSON API plugin, so I want the root of the site to hit my Grunt directory (located at site/dist/index.html), but all requests to siteurl.com/api to hit the index.php file and proceed normally.
Keep in mind I have other assets / images located in this site/dist directory, so ideally, it would be awesome if all requests to the site root would simply use this folder as the "base" of the site (e.g. a request to siteurl.com/images/testimage.jpg pulls from site/dist/images/testimage.jpg).
I feel like I'm onto something here and am surprised I couldn't find anything that directly tackles this issue.
What I've done now is renamed the index.php from WordPress to index-api.php and left it the same:
index-api.php:
<?php
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wordpress/wp-blog-header.php');
// phpInfo();
.htaccess:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ index-api.php [L]
RewriteRule (.*)$ site/dist/index.html [L]
</ifModule>
I tried a myriad of other efforts from a few posts trying to get this working, and it seems to me like it should work fine. The funny thing is, if I comment out the last line RewriteRule (.*)$ site/dist/index.html [L] the api request works normally as expected, so I know I'm close.
Any suggestions?
Would appreciate anyone's help on this, it's been really confusing!
In the first place you'll need to make sure that requests made to /index-api.php are not matched and rewritten by the second rule. In the second rule you can use $1. $1 will be replaced with whatever was matched in the first capture group. We'll also need to make sure that the second rule will not match what it rewrites, or we'll end up with an infinite loop and an internal error.
You can use the $1 in the first rule too, as I show below:
RewriteRule ^api/(.*)$ index-api.php?url=$1 [L]
RewriteCond %{REQUEST_URI} !^/site/dist/
RewriteCond %{REQUEST_URI} !^/index-api\.php
RewriteRule (.*)$ site/dist/$1 [L]
I recommend reading the documentation of mod_rewrite to get a better understanding how you can use it and what things you have at your disposal while rewriting url's.

Categories