Forum Moderators: phranque

Message Too Old, No Replies

Having Trouble with mod rewrite

Trying to find out if using mod_rewrite is the right solution to my problem

         

HellaBAD

12:42 am on May 10, 2012 (gmt 0)

10+ Year Member



Hello! This is my first time posting and I'm relatively inexperienced when it comes to fiddling around with Apache, so I was hoping someone here might be able to help guide me in the right direction.

What I believe I want to do is create a rewrite in .htaccess for my site so that when a user asks for URL A, the content is served from URL B, but the user still sees the URL as being URL A. That said, I know at least that what I don't want is a redirect.

As an example, let's say I have content at
mydomain.com/projects/project-example
. I want users to be able to ask for
mydomain.com/project-example
, the browser should display the content from
mydomain.com/projects/project-example
, and the address in the URL should remain
mydomain.com/project-example
. I only want this to be done for any page that's prefaced with
/projects/
.

To provide a little background, my site is hosted on MediaTemple shared hosting and I'm using Wordpress for the CMS.

Here's what I've implemented in the .htaccess file in an attempt to achieve my desired results:


RewriteEngine On
RewriteRule ^project-example$ /projects/project-example [L]



After implementation, when asking for
mydomain.com/project-example
, I was effectively served the content from
mydomain.com/projects/project-example
, but unfortunately the URL still displays
mydomain.com/projects/project-example
, almost as if I'd just implemented a redirect. Is it possible Wordpress or something else is interferring, or have I gone about this the wrong way?

I've trawled through several mod_rewrite/mod_alias tutorials & FAQs, but am still having trouble wrapping my head around the issue. I'm really stumped.

If a rewrite isn't the correct solution, would a symbolic link applied only to
/projects/
be a better one? Is such a thing possible?

I'd love to hear any advice you can give. Thanks!

lucy24

12:55 am on May 10, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is it possible Wordpress or something else is interfering

Short answer: yes ;)

Other than that, it sounds as if you are exactly on the right track and you understand what you're doing.

Watch this space. I gotta run, but someone will swing by with further ideas.

g1smd

1:02 am on May 10, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're missing a step.

When you implement a rewrite, the URL request comes in and mod_rewrite "translates" that request into an internal server filepath to get the content from.

That is, it doesn't "serve content from another URL". URLs are used "out there" on the web and files and paths are used "here" inside the server. They are not at all the same thing. They are merely "related" by the server configuration.

So, the left side of your rule (the RegEx pattern) is correct in that it matches a request for example.com/project-example but the right side of the rule is incorrect because it is unlikely that you have an extensionless file called project-example in the /projects/ folder waiting to serve some content. It's likely called index.php and it's probably expecting some appended parameters to tell it what to pull from the database where the content is really stored.

HellaBAD

2:07 am on May 10, 2012 (gmt 0)

10+ Year Member



Hmm, I think I understand what you're saying, g1smd. Looking at the rest of my .htaccess file, Wordpress is definitely using a rewrite to keep the index.php file extension from being seen in the url. So, it's exactly as you said lucy24.

I still don't fully understand what needs to change on the right side of the rule yet, but I'll pick up where I left off and keep working at it again tomorrow.

Thanks for your helpful responses!