RewriteRule URL's remove extension's.

Discuss practical ways rearrange URLs using mod_rewrite.

RewriteRule URL's remove extension's.

Postby xxx47 » Wed Oct 22, 2008 1:35 am

Hi guys!.

I have a problem. I use CSS but does not work with this rule:
.htaccess:
Code: Select all
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.php.*
RewriteRule ^(.*)$ /index.php?sec=$1 (extension out)


Solution ¿?, add "/" ^/(.*)$
Code: Select all
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.php.*
RewriteRule ^/(.*)$ /index.php?sec=$1 (extension out)

CSS work! but.. the url's does not work (404).

I need to use url's, but remove all extension's
example:
http://www.domain.com/seccionone
http://www.domain.com/secciontwo
etc.

Sorry, but I need more examples.. plzz :(
Thanks.
xxx47
 

Postby laisfun » Wed Oct 22, 2008 2:05 am

If you're trying to go use a friendly URL like you provided then your RewriteRule will look like this...

e.g., http://www.domain.com/sectionone

RewriteRule ^([^/]+)/?$ /index.php?sec=$1 [QSA,L] # or try just [L]

if you need to remove the word at the end of section then it would look
like this...

RewriteRule ^section([^/]+)/?$ /index.php?sec=$1 [QSA,L] # or try just [L]
laisfun
 
Posts: 16
Joined: Sun Aug 27, 2006 7:03 pm
Location: CA, USA

Postby xxx47 » Wed Oct 22, 2008 2:37 am

Thanks laisfun,

CSS dont work, but url's yes..
.htaccess:
Code: Select all
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.php.*
RewriteRule ^([^/]+)/?$ /index.php?sec=$1 [QSA,L]


And.. xHTML file (tag base / > for css).
<base href="http://www.domain.com/" />
also does not work..
xxx47
 

Postby xxx47 » Wed Oct 22, 2008 6:23 am

Hi,
For.. http://www.domain.com/sectionone
Works 100%!:
Code: Select all
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?sec=$1 [QSA,L]


But now, I need to add one more value..
Example nav web site (without end /) I like:
http://www.domain.com/sectionone
http://www.domain.com/sectionone/other
http://www.domain.com/sectiontwo
http://www.domain.com/sectiontwo/other
etc.

I can't for all.. see.
Code: Select all
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /index.php?sec=$1&id=$2 [QSA,L]


Only works for:
http://www.domain.com/sectionone/other..
etc.

but, when I use the first example-link
http://www.domain.com/sectionone
doesn't work, requires slash (/)
http://www.domain.com/sectionone/ (i don't like).

Please, help me :(
Thanks all.
xxx47
 

Postby richardk » Wed Oct 22, 2008 7:36 am

Sorry xxx47, i seem to have deleted your account.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([^/]+))?/?$ /index.php?sec=$1&id=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby David » Wed Oct 22, 2008 11:06 am

Hi again.

Thanks Richardk!! works :) but I have a question..

This code:
Code: Select all
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([^/]+))?/?$ /index.php?sec=$1&id=$2 [QSA,L]


The users can access all url's..
http://www.domain.com/sectionone (only pass)
http://www.domain.com/sectionone/ (slash end, remove/deny)
And..
http://www.domain.com/sectionone/other (only pass)
http://www.domain.com/sectionone/other/ (slash end, remove/deny)

How I can remove/deny the last slash?

Thanks.
David
 
Posts: 2
Joined: Wed Oct 22, 2008 10:47 am

Postby laisfun » Wed Oct 22, 2008 11:45 am

David,

Keep in mind, that dropping the slash will affect your chances in getting into the DMOZ directory since they can obviously see that your site is being redirected. It's against their policy to allow sites that redirect; therefore, I finally got listed by DMOZ when I decided to end my URI with a slash. If it doesn't matter to you then you'll drop the /? which means that the URI can either have the slash or not.
laisfun
 
Posts: 16
Joined: Sun Aug 27, 2006 7:03 pm
Location: CA, USA

Postby laisfun » Wed Oct 22, 2008 11:48 am

RichardK,

What does the colon mean, in...
Code: Select all
(?:/([^/]+))?


Thanks!
laisfun
 
Posts: 16
Joined: Sun Aug 27, 2006 7:03 pm
Location: CA, USA

Postby richardk » Wed Oct 22, 2008 1:32 pm

How I can remove/deny the last slash?

As laisfun said, remove the "/?".

What does the colon mean, in...
Code: Select all
(?:/([^/]+))?

?: means don't create a backreference ($n). More info.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby David » Thu Oct 23, 2008 12:41 pm

Works!

Thanks Richardk and Laisfun ;)
David
 
Posts: 2
Joined: Wed Oct 22, 2008 10:47 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 25 guests

cron