I use Wordpress as a CMS, where the front page is static for the most part and the actual blog part of it has been moved to a new page that uses a custom page template that basically acts as if it were the front page on a standard Wordpress install. It works pretty well but it still has a few bugs. The main one is that pagination will no longer work, Wordpress won't recognize the page URLs that are generated.
So far I've configured Wordpress to display the correct URLs (domain.com/blog/page/2 instead of domain.com/page/2), and I've also gotten it to display a specific page of blog posts via a URL parameter (domain.com/blog?page=N). So basically I'm almost done, I just need to map domain.com/blog/page/N to domain.com/blog?page=N. This would be an easy task if it were just a plain old static site, but Wordpress has it's own URL handling that I believe may be interfering with it - not to mention "blog" isn't an actual URL so it needs to go through the rules twice, once for mapping the blog pagination, and once again so Wordpress can handle the generated URL as it would regularly... I think I may need to use the N or NS flags for the rewrite rules but I just don't know how I would use them
The rule I've come up with:
RewriteRule ^/blog/page/([0-9]+)$ /blog?page=$1
Default Wordpress .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
As per some suggestions, I've gotten this for the .htaccess, but it still doesn't recognize it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule /blog/page/([0-9]+)$ /blog?page=$1 [N,QSA]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
And I'm fairly certain changing the page via the URL parameter does work - I change the number in the URL manually and it does in fact change the page. The web server is LiteSpeed, if that makes any difference
Try the following
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule /blog/page/([0-9]+)$ /blog?page=$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I figured it out eventually (a few days after -- I just forgot about this until now). First, make sure you have Wordpress updated. At the time I was using 2.5 or 2.6 but for some reason it didn't work until I updated.
First, create a custom template. You will use this template for your homepage so customize it and all that jazz. Leave the "Main Index" (index.php) template alone -- that template will be used for the blog index page so it needs the WP loop to list your blog posts.
Now, go and create two pages. One for your home page (let's call it "home") and another for your blog (let's call it "blog"). On the "home" page, set the the template to the homepage template you created previously. On the blog page, leave it on the default template -- it will use the "Main Index" template by default.
Now, go into your "Reading" settings and change it to "A static page." For the front page, select the "home" page you created previously. For the posts page, select the "blog" page you created.
That should be it. I had actually tried this beforehand but it did not work so I tried using redirect rules, to no avail. I ended up being an out of date WP install. If you have a sitemap mod installed, make sure you exclude the "home" page so the spiders don't pick up a duplicate of your home page at example.com/home. You can also put it in your robots.txt to be sure.
Once you're done, the root (example.com/) of your domain (assuming you have WP installed there) will point to your "home" page. Your blog page (example.com/blog) will list your posts. Pagination will work as expected (example.com/blog/page/2 etc.).
Or try
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^blog/page/([0-9]+)$ /blog?page=$1 [NC,N]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [NC,L]
</IfModule>
The NC makes it case insensitive, the N make it reapply the rules, and the L stops the rule processing.
Related
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>
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.
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 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.
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