I am currently creating a new website which uses custom PHP code, but i am retrieving Wordpress posts too from a remote wordpress.
In my root folder (as wordpress does), i have the default wordpress .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 also have a index.php file which gives me the posts from WP:
<?php
// Include the wp-load'er
include('wp/wp-load.php');
// Get the last 10 posts
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 10,
'post_status' => 'publish'
));
...
?>
In my project i also have other custom php files like search.php, detail.php etc.
All Wordpress posts links are formatted using slugs like: www.mywebsite.com/my-post/
The question is, how can I change .htaccess file in order to pass index.php a slug parameter when the URL is not a .php file?. (Like index.php?slug=my-post)
Thanks.
You can try something like that. I hope that help you.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^slug=(.*)$
RewriteRule ^(.*)$ http://www.mywebsite.com/page/%1.html [R=302,L]
# 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 WordPres
Thats the solution!:
RewriteEngine On
RewriteRule ^(.*)/$ /index.php?slug=$1 [L]
# 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 WordPres