Could someone check my newbie code

New to mod_rewrite? This is a good place to start.

Could someone check my newbie code

Postby Samwise » Sat Jun 13, 2009 1:58 am

Hey,

as I'm new to mod_rewrite I'd like someone to check my code as I'm not sure of it and one part is not working as intended (description below the code)

Code: Select all
# Redirect
Redirect 301 /panel http://panel.localhost.pl
Redirect 301 /forum http://forum.localhost.pl
Redirect 301 /wiadomosci http://wiadomosci.localhost.pl

IndexIgnore *
Options All
Options +FollowSymLinks -Indexes

RewriteEngine On

# WWW Remove
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]

# Remove index.php
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC,OR]
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\/.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /$1 [NC,R=301,L]

# Rewrite all to index.php
# Ignore existing files.
RewriteCond %{SCRIPT_FILENAME} -f [OR]
# Ignore existing directories.
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule . - [PT,L]
RewriteRule .* /index\.php [QSA,L]


Now, the problem is in removing index.php. For example:

OK - http ://localhost.pl/index.php?var=val to http ://localhost.pl/?var=val
BAD - http ://localhost.pl/index.php/val1/val2 does not removing the index.php


And I'm not sure about the last rule and rewrite - the one that is rewriting everything (except existing folders and files) to index.php

for example:
http ://localhost.pl/news/50/3
is rewriting to index.php but without redirect so the adres is still visible in the browser and I'm dealing with all the variables in php by $_SERVER.

Cheers,
Sam

P.S.
Sorry for probably my poor english :D
Samwise
 
Posts: 2
Joined: Fri Jun 12, 2009 4:25 pm

Postby richardk » Sat Jun 13, 2009 2:40 pm

BAD - http ://localhost.pl/index.php/val1/val2 does not removing the index.php

What should happen?

Try
Code: Select all
Redirect 301 /panel http://panel.localhost.pl
Redirect 301 /forum http://forum.localhost.pl
Redirect 301 /wiadomosci http://wiadomosci.localhost.pl

IndexIgnore *
Options All
Options +FollowSymLinks -Indexes

RewriteEngine On

# WWW Remove
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]

# Remove index.php
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php((\?|/).*)?\  [NC]
RewriteRule ^(.+/)?index\.php(/.*)?$ /$1 [NC,R=301,L]

# Rewrite all to index.php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . /index.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 111 guests

cron