Removing part of a pretty URL with mod_rewrite

Discuss practical ways rearrange URLs using mod_rewrite.

Removing part of a pretty URL with mod_rewrite

Postby memerson » Tue Jun 09, 2009 7:30 am

Hi,

I've been trying to figure this problem out for a while now but I don't seem to be getting anywhere.

Basically I have a Wordpress site which only allows certain levels of permalinks - using pretty URLs - but I want to be able to mod_rewrite it so I can literally put anything I want within in a level but it be removed by the htaccess.

For example:

Code: Select all
http://mywordpresssite.com/page/keyword/id/


The 'keyword' can be anything, something that pertains to that page. However, the 'id' must remain there in order for the page to dynamically display the correct content. So if I write:

Code: Select all
http://mywordpresssite.com/webhosting/blog-hosting/123


This would actually be interpreted as:

Code: Select all
http://mywordpresssite.com/webhosting/123


And the 123 would actually display the "blog hosting" page. Hypothetically.

I've currently come up with

Code: Select all
RewriteCond %{THE_REQUEST} ^[A-Z]{3}\ /webhosting/[A-Za-z0-9]?-(.*)\/(.*)\ .*HTTP [NC]
RewriteRule .* /webhosting/$1 [R,NE,R=301,L]


But, to be honest, I'm really stuck and not quite sure what all of it means! I simply want to be able to grab an id from the URL but remove the "keyword" part of it - I only want the keyword there for SEO purposes.

Thanks in advance - any help is appreciated.

Michael
memerson
 
Posts: 11
Joined: Tue Apr 17, 2007 6:04 am

Postby richardk » Tue Jun 09, 2009 3:26 pm

You want /webhosting/blog-hosting/123 in the browser's address bar the whole time?
What is the Wordpress mod_rewrite (is it the standard one)?

If it is the standard one, you will need to do it in Wordpress as it uses REQUEST_URI from the browser address bar.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby memerson » Wed Jun 10, 2009 12:37 am

richardk wrote:You want /webhosting/blog-hosting/123 in the browser's address bar the whole time?


Yes. There is a page on my site called "hosting" which is accessible via www.mysite.com/hosting/ but what I want to be able to display is www.mysite.com/hosting/blog-hosting/123/ (as an example). Basically if Wordpress detects the word "hosting" it should just go straight to that page - but my PHP in the template will use the id I pass to display what it needs to.
What is the Wordpress mod_rewrite (is it the standard one)?


I believe so. Here is the actual htaccess file (without my failing code!):

Code: Select all
RewriteBase /
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress



If it is the standard one, you will need to do it in Wordpress as it uses REQUEST_URI from the browser address bar.


Yeah I know that - but it seems Wordpress will throw up a 404 because it can't find the page - I don't know how to write a rule that will move to the right page and still keep the browser address. And I'm not talking about re-direct, it's more like "require". I don't want a 301 really.

Thanks for your reply.
Michael
memerson
 
Posts: 11
Joined: Tue Apr 17, 2007 6:04 am

Postby richardk » Wed Jun 10, 2009 1:19 pm

Your mod_rewrite needs to be
Code: Select all
Options +FollowSymLinks

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

only.

Yeah I know that - but it seems Wordpress will throw up a 404 because it can't find the page - I don't know how to write a rule that will move to the right page and still keep the browser address. And I'm not talking about re-direct, it's more like "require". I don't want a 301 really.

The Wordpress mod_rewrite sends all requests to index.php (that aren't for files or directories that exist). Mod_rewrite has no control over anything else (it just has /index.php not /index.php?some=control). Then Wordpress uses the REQUEST_URI variable from the browser's address bar which mod_rewrite can't change to find the right page content. You need to ask Wordpress support, i'm almost certain you can create custom Wordpress "RewriteRules".
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 27 guests

cron