- This topic is empty.
-
AuthorPosts
-
November 14, 2012 at 3:16 pm #40805foomanParticipant
I am having a bit of trouble with some mod_rewriting.
I have a url such as:
http://www.example.com/about-us/deeper-linkI’d like to convert this on the server to:
http://www.example.com/about-us?slug=deeper-linkRewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^/index.php$ - [L]
RewriteRule ^about-us/([^/.]+)/?$ about-us?slug=$1 [L]Now the issue is that unless I create a directory called “about-us”, I get a 500 error.
I’d like to just have a page named about-us, not another directory.
So I make a directory called “about-us”, add an index file in there as the page, and add a trailing slash to the rule.After doing that, it works.
RewriteRule ^about-us/([^/.]+)/?$ about-us/?slug=$1 [L]
So I guess my question is, how do I get this to work as expected without a directory… just rewrite it to a simple file…?
November 15, 2012 at 7:36 am #114412lhoezeeMemberYou are really close..
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^/index.php$ - [L]
RewriteRule ^about-us/([^/.]+)/?$ index.php?slug=$1 [L]
# added this line just in case someone goes to about-us without anything after it.. so you do not get a 404
RewriteRule ^about-us/?$ index.php?slug=$1 [L]
November 15, 2012 at 11:34 pm #114520lhoezeeMemberSorry for the delay, I thought I would have received an email from your response..
Usually, most of the sites I do run off of just the index file and I use mod rewrite to dynamically include other files or “content” based on the URL which then I’ll have a controller which handles template calls on the fly etc.
If I’m understanding correctly, does this work for you?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
# Note: This will add a .php to the end of the slug value
# example:
# /about-us/test will actually be: /about-us?slug=test.php
RewriteRule ^(.*)$ $1.php
RewriteRule ^/index.php$ - [L]
RewriteRule ^about-us/([^/]+) about-us.php?slug=$1 [NC,L]
RewriteRule ^about-us about-us.php [NC,L]
That works on my side. I created an about-us.php file in my root directory and went to /about-us/test which then created /about-us?slug=test.php
I wasn’t sure if you wanted the .php appended at the end so I added a note to one of the Rules above.
Luke
November 15, 2012 at 11:42 pm #114522lhoezeeMemberI found where I can get email notifications :)
November 17, 2012 at 9:25 pm #114631lhoezeeMemberOk, apparently even though I checked the box for emails, I didn’t get the notification of your comment and I checked in spam, strange.
Give this a try:
**Note: ** This assumes the first ([^/]+) is an actual static file. so for example:
/about-us/test will actually be:
about-us.php?slug=testand
/contact-us/test2 will actually be:
contact-us.php?slug=test2If the file does not exists a 404 will be generated.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
# Note: This will add a .php to the end of the slug value
# example:
# /about-us/test will actually be: /about-us?slug=test.php
RewriteRule ^(.*)$ $1.php
RewriteRule ^/index.php$ - [L]
RewriteRule ^([^/]+)/([^/]+) $1.php?slug=$2 [QSA,L]
-
AuthorPosts
- The forum ‘Other’ is closed to new topics and replies.