homepage Welcome to WebmasterWorld Guest from 54.234.225.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
.htaccess help
Pico_Train




msg:4212929
 10:00 am on Oct 7, 2010 (gmt 0)

Hi there!

I am trying to write a site with cleanish urls. I have an .htaccess file that works fine for the interior pages but not the index page.

My urls look like this:

http://test.localhost/page/index

http://test.localhost/page/exmaple-page

If i go to http://test.localhost it bombs out but I would like it to find the content at http://test.localhost/page/index

I have tried a few things but can't get it, my sample .htaccess file is below:

# Internally rewrite search engine friendly static URL to dynamic filepath and query

RewriteRule ^page/([^/]+)/?$ content.php?page=$1 [L]

# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /content\.php\?page=([^\ ]+)\ HTTP/
RewriteRule ^content\.php$ http://test.localhost/page/%1? [R=301,L]


Thanks in advance for your help!

[edited by: jdMorgan at 2:59 pm (utc) on Oct 7, 2010]
[edit reason] De-linked URLs for readability. [/edit]

 

jdMorgan




msg:4213107
 3:06 pm on Oct 7, 2010 (gmt 0)

> If i go to http://test.localhost it bombs out but I would like it to find the content at /page/index

Then you will need another rule, because requests for URL-paths that do not start with "/page/" will not be matched by your current first rule, and so will not be rewritten to your script's filepath. Further, this new rule will need to explicitly insert "index" as the "page=" value.

I advise that you comment-out your current #2 rule until you get your #1 rule and the required new rule working. This will make debugging of problems with the internal rewrite rules simpler. Also, note that all external redirect rules should be placed before any internal rewrite rules, so technically, your current #2 rule should go first.

Jim

Pico_Train




msg:4213671
 9:53 am on Oct 8, 2010 (gmt 0)

OK, thanks will give a go shortly and post again if I can't get there.

Pico_Train




msg:4215154
 2:51 pm on Oct 11, 2010 (gmt 0)
Hello,

Gave a shot and this is what I have. Any issues apparent with what I have done?

Options +FollowSymLinks
RewriteEngine On
RewriteBase /


#rule 1
RewriteRule ^$ content.php?page=index

#rule 2
# Internally rewrite search engine friendly static URL to dynamic filepath
#and query
# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /content\.php\?page=([^\ ]+)\ HTTP/
RewriteRule ^content\.php$ http://test.localhost/page/%1? [R=301,L]


#rule 3
RewriteRule ^page/([^/]+)/?$ content.php?page=$1 [L]

I also tried to add this in to redirect

http://test.localhost/page/index to http://test.localhost for dup url purposes but it ends up redirecting to http://test.localhost/?page=index

This is what I did:

redirect 301 /page/index http://test.localhost/

The end result there is not so good.

Thanks for your help!
jdMorgan




msg:4217246
 4:46 pm on Oct 15, 2010 (gmt 0)

Won't work due to rules out of order, mixed use of mod_alias and mod_rewrite, and missing [L] flag.

Also, some comments are "very wrong."

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#
# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /content\.php\?page=([^&\ ]+)\ HTTP/
RewriteRule ^content\.php$ http://test.localhost/page/%1? [R=301,L]
#
# Externally redirect requests for /index to "/"
RewriteRule ^index$ http://test.localhost/ [R=301,L]
#
# Externally redirect non-canonical hostname requests to canonical hostname
# (update this when you move the site to a real domain)
RewriteCond %{HTTP_HOST} !^(test\.localhost)?$
RewriteRule ^(.*)$ http://test.localhost/$1 [R=301,L]
#
# Internally rewrite search engine friendly static home page URL to dynamic filepath and query
RewriteRule ^$ content.php?page=index [L]
#
# Internally rewrite other search engine friendly static URLs to dynamic filepath and query
RewriteRule ^page/([^/]+)/?$ content.php?page=$1 [L]

  • Do not use mod_alias Redirect or RedirectMatch directives on this server; If you use mod_rewrite for anything, then use it for everything. Otherwise the server --and not you-- determines what order those directives will get executed in. This can lead to bad problems with search listings and rankings.
  • Put your rules in order with all external redirects first, from most-specific patterns and conditions to least-specific patterns and conditions. Follow these with all of your internal rewrites, again in order from most- to least-specific. Access-control rules, if any, generally should go before the redirects (There's no benefit from redirecting unwelcome visitor requests, and you certainly don't want to rewrite those requests).
  • Always end your rules with the [L] flag, unless you know why you don't want to (these cases are very rare).

    Jim

  • Pico_Train




    msg:4224498
     12:03 pm on Oct 31, 2010 (gmt 0)

    Thanks Jim, will have a look at this again when I get a chance to digest it.

    Global Options:
     top home search open messages active posts  
     

    Home / Forums Index / Code, Content, and Presentation / Apache Web Server
    rss feed

    All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
    Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
    WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
    © Webmaster World 1996-2014 all rights reserved