I have a custom URL like this:
website.com/show/?id=9999&n=page-name
I'm trying to come up with a rewrite rule to convert it to:
website.com/show/9999/page-name/
Note that this is a WordPress site and /show/ is WP page name.
Here's the rules I'm using in .htaccess:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^show/(.*)$ /show/?id=$1 [R=301,NC,QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This is working; it rewrites website.com/show/?id=9999 to website.com/show/9999/.
Then I modified the rule for the second query string:
RewriteRule ^show/(.*)/(.*)$ /show/?id=$1&n=$2 [R=301,NC,QSA]
But now website.com/show/9999/page-name/ returns a 404 error. It works if I go to: website.com/show/9999/?n=page-name.
What am I doing wrong?
Update
The 404 problem is now solved.
However, now I need to redirect the old query string URL:
website.com/show/?id=9999&n=page-name
to the new SEO friendly URL:
website.com/show/9999/page-name
How do I setup that redirect?
Try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^show/(\d+)/?([^/]*)/?$ /show/?id=$1&n=$2 [L,NC,QSA]
# Wordpress defaults:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
## Results
# show/9999 => show/?id=9999&n=
# show/9999/ => show/?id=9999&n=
# show/9999/page-name => show/?id=9999&n=page-name
# show/9999/page-name/ => show/?id=9999&n=page-name
As you see, when page-name is not available, there will be an empty n query string parameter. In your PHP script, instead of checking for the parameter presence using isset(), try using empty().
One last note; The above rules do not redirect the request, it maps the request under the hood, so that the URL stays clean. If you need a redirect, just add a R flag.
404
Even if you remove your custom rewrite rules, you'll get a 404 from WordPress. That's because, as you said, the rewrite target (/show) is a WordPress page and ultimately get mapped to the index.php file. Then, WordPress checks its database to see if it can find a page with that path or throws a 404 if it can't. Obviously, the latter is your case.
Update
Regarding your recent update; To redirect the old URL to the new one, you need some rewrite rules along the lines of:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=(\d+)(&n=)?(.*)$
RewriteRule ^show/?$ /show/%1/%3 [R=301,QSD,L]
# Wordpress default rules:
# ...
</IfModule>
## Results
# show?id=9999 => show/9999
# show?id=9999&n=page-name => show/9999/page-name
# show/?id=9999&n=page-name => show/9999/page-name
Don't use [R=301] until you're 100% sure that everything's working fine, as it's aggressively get cached by the browser.
I think I figured it out... almost.
#sepehr is correct that WordPress is checking, not finding the page, and throwing a 404. So I needed to use WordPress' own rewrite engine to define the rewrite rules so it recognizes what I'm doing.
So I added this to my WordPress theme's functions.php file:
add_action( 'init', 'init_custom_rewrite' );
function init_custom_rewrite() {
add_rewrite_rule(
'^show/([^/]*)/([^/]*)/?',
'index.php?page_id=2382&id=$matches[1]&n=$matches[2]',
'top'
);
}
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'id';
$vars[] = 'n';
return $vars;
}
Now the URL website.com/show/9999/page-name works correctly, not throwing a 404.
However, now I need to redirect the old query string URL to this new one. See my updated question.
Update
Here's the rewrite rule to redirect the old query string URLs to the new SEO friendly URLs:
RewriteCond %{QUERY_STRING} ^id=([^/]*)&n=([^/]*)$
RewriteRule ^show/?$ /show\/%1\/%2\/? [R=301,L]
Please check below rule in to your .htaccess.
RewriteRule ^show/(.+)/(.+)$ show/?id=$1&n=$2 [L,QSA]
I am using an .htaccess file to redirect "http://www.domain.com/cars/cars.php?cars_item=231" to "http://www.domain.com/cars/231/porsche"
So basically I'm redirecting 'cars.php?cars_item=231' to '$id/$title'
The .htaccess file consists of:
#Start
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect cars.php?cars_item=231 to cars/231/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+cars/cars\.php\?cars_item=([^&\s]+) [NC]
RewriteRule ^ /cars/%1 [R=302,L]
# Internally forward cars/231/ to cars.php?cars_item=231
RewriteRule ^cars/([0-9]+) /cars/cars.php?cars_item=$1 [NC,L,QSA]
#end
Then i'm using the following hrefs:
"http://domain.com/index.php" = echo porsche;
and in "http://www.domain.com/cars/cars.php" i'm using a $_GET['cars_item'];
My question is: How could I remove the $id from the url please? I want to have the following url as a result - "http://www.domain.com/cars/porsche" ... so basically I want to update the .htaccess and redirect "http://www.domain.com/cars/cars.php?cars_item=231" to "http://www.domain.com/cars/porsche"
This is because the $id in the url is creating some problems for indexing especially. Google keeps crawling "http://www.domain.com/cars/$id/" where as the $id is not a folder but is a db row hence I'd like to remove the $id from the url.
I would appreciate your help as always.
I recently had the following answered: .htaccess rewrite url does not work
I would like to now add the title from the db for each id to the URL too. From what I've read on the internet it seems that it is possible. Mostly I have $title in each php file which refers to the title in the db.
Currently I'm using this in my current .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect cars.php?cars_item=231 to cars/231/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+cars/cars\.php\?cars_item=([^&\s]+) [NC]
RewriteRule ^ /cars/%1 [R=302,L]
# Internally forward cars/231/ to cars.php?cars_item=231
RewriteRule ^cars/([0-9]+)/?$ /cars/cars.php?cars_item=$1 [NC,L]
And the following as href: <a href=\"cars/$id\"/>$title</a>.
So what I wish to have as my URL is the following. Basically I need to add the title to the current URL and replace the spaces between words in title with dashes.
http://www.domain.com/cars/543/new-porsche-is-out
cars -> subfolder
543 -> $id (From the database)
new-porsche-is-out -> $title (From the database)
You can just change your last rule to allow title in the end:
# Internally forward cars/231/title to cars.php?cars_item=231
RewriteRule ^cars/([0-9]+) /cars/cars.php?cars_item=$1 [NC,L,QSA]
I have a running service that lets my user choose their own URL address (example: http://hsdfhdfghfh.com/theUserURL)
Now I'm going to put my front site on a wordpress but keep my app running along-side with it.
Now for the problem, i've set a rule in htaccess to forward my users url to their page, but wordpress also needs the same rule in order to make pretty URL (permalink).
Here is my htaccess with my code and the code that wordpress injected. Currently only my redirect is working, how can I make them both work together?
Can I add a code in my php file that gets the URLs and if it has no entries I throw it to wordpress to show?
This is my htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./url.php?w=$1
AddDefaultCharset UTF-8
# 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
And this is my php (url.php) file that handles the redirects:
<?php
session_start();
$url_name = $_GET['w'];
include ('init.php');
$result = mysql_query("SELECT * FROM urls WHERE url='$url_name'");
$num_rows = mysql_num_rows($result);
$query_result = mysql_fetch_assoc($result);
if($num_rows == 0){
include($theurl.'/404.php');
exit();
}
$_SESSION['url_id'] = $query_result['url_id'];
?>
..Shows the rest of the page for the user with the specified url_id session...
Maybe instead of showing the 404.php page I could transfer the entered URL to wordpress somehow?
Cheers!
After trying different approaches and trying to manipulate using .htaccess I realized that this process is very bad for loading times and overloading the server.
I ended up moving Laravel to a subdomain :/
Don't try to put Wordpress and Laravel together on the same domain unless you really want to tweak wordpress to the core.
I had problems with WordPress migration from one server to another, and generally now it works but I have many smaller problems..
Before I use domain oldexample.com and now i use domain newexample.com/something . Generally everything on the page works but changing language isn't work (qTranslate plugin).
I think that I found the reason of this - in admin menu I found in some places situation that href links start from "/" for example: "/wp-admin/..." and in result it change the URL from newexample.com/something/wp-admin to newexample.com/wp-admin. I see this problem in qTranslate settings links and when I want delete some plugins. I get the error message "404 Not Found - nginx/1.4.5" in results...
Did you see this problem before? Maybe I should change something in WordPress core files? .htaccess? Now it looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?newexample\.com\/something$ [NC]
</IfModule>
# 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
After long time I find the solution! All "href" are generated by using $_SERVER['REQUEST_URI'], and this variable show only the rest part of the path (without /something/. I add to wp-settings.php this part of code: $_SERVER['REQUEST_URI'] = '/something'.$_SERVER['REQUEST_URI']; (on the very beggining) all forms generate good path.