URL wont work: Problems with same folder name and file name

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

URL wont work: Problems with same folder name and file name

Postby badlydrawnrob » Wed Jul 23, 2008 8:56 am

I have been pulling my hair out over this,

I am new to mod_rewrite and searched for hours to try and find a simple solution to remove .html and .php file extensions.

I thought I had found the solution below:

Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Remove trailing slashes.
# e.g. example.com/foo/ will redirect to example.com/foo

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.+)/$ /$1 [R=301,QSA]


# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.html -f

RewriteRule ^(.+)$ $1.html [L,QSA]


# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.php -f

RewriteRule ^(.+)$ $1.php [L,QSA]


On the most part it works perfectly but the problem is if you have a folder and file name at yoursitename.com/folder you just get sent to the index of that folder.

How would I force the mod_rewrite to point to the .html file "folder" instead of the folder named "folder"?!

Also do I need to add a trailing slash "/" onto the url or is the removal of trailing slash code above fine? I would need the code for addition of trailing slash if this is the case.

Thanks in advance
badlydrawnrob
 
Posts: 7
Joined: Wed Jul 23, 2008 8:45 am

Another RewriteRule helps but not ideal

Postby badlydrawnrob » Wed Jul 23, 2008 10:43 am

This I found seems to help by overriding the folder and pulling in the html file, I think I know how it works but is there anything better or more graceful then having to add all sub folders like this?

So yoursitename.com/folder/ is grabbing content from yoursitename.com/folder.html

Sorry if Im a complete noob dunce ;)

Code: Select all
# Redirect from folder to .html file

RewriteRule ^what-we-do/$ what-we-do.html [L]
badlydrawnrob
 
Posts: 7
Joined: Wed Jul 23, 2008 8:45 am

Still doesnt help...

Postby badlydrawnrob » Wed Jul 23, 2008 12:54 pm

The code above kinda works, but any links on the page dont work unless a <base href="" /> is specified in the head.

Plus I have <a name=""> anchor tags at different points of the page, none of which will work if above code is added!!

any ideas anyone?
badlydrawnrob
 
Posts: 7
Joined: Wed Jul 23, 2008 8:45 am

Postby richardk » Thu Jul 24, 2008 2:27 pm

Try
Code: Select all
Options +FollowSymLinks
DirectorySlash Off

RewriteEngine On

# Add trailing slashes to directories (as long as there is not HTML or PHP file of the same name).
RewriteCond %{SCIRPT_FILENAME}.html !-f
RewriteCond %{SCIRPT_FILENAME}.php !-f
RewriteCond %{SCIRPT_FILENAME}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Remove trailing slashes from HTML and PHP file requests (when there is no extension)
RewriteCond %{SCIRPT_FILENAME}.html -f [OR]
RewriteCond %{SCIRPT_FILENAME}.php -f
RewriteRule ^(.+)/$ /$1 [R=301,L]

# Rewrite if a HTML file exists.
RewriteCond %{SCIRPT_FILENAME}.html -f
RewriteRule ^(.+)$ /$1.html [QSA,L]

# Rewrite if a PHP file exists.
RewriteCond %{SCIRPT_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [QSA,L]


It uses DirectorySlash to stop Apache adding trailing slashes to directories, but mod_rewrite should add them when a HTML or PHP file of the same name does not exist. There is a warning, however
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash wrote:Security Warning

Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Nearly there....

Postby badlydrawnrob » Fri Jul 25, 2008 4:13 am

Thanks for that,

I havent tried it yet (as I didnt expect a kind response so soon) but managed to get this working:

Code: Select all
# Redirect 404 Error
ErrorDocument 404 /404.html

Options +FollowSymLinks
RewriteEngine On

# Redirect www. Domain URL

RewriteCond %{HTTP_HOST} ^www\.(yoursite\.com)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://yoursite.com/ [R=301,L]


# ADDING Trailing Slash:
# THIS IS THE CAUSE OF PROBLEM I THINK!

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.+)$ /$1/ [L,R=301]


# Redirect from folder to .html file

RewriteRule ^what-we-do/$ what-we-do.html [L]


# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f

RewriteRule ^(.+)/$ $1.html [L,QSA]


# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f

RewriteRule ^(.+)/$ $1.php [L,QSA]



Now this works perfectly but there are still two problems:

One:

    The 404 ERROR redirect works for URLs in the root yoursite.com/pagenothere but throws out a loop when looking for a URL that is redirected to a file like this:

    yoursite.com/somefolder/file/pagenothere loops like this
    yoursite.com/somefolder/file/pagenothere.html.html.html.html.html.html.html

Two:

    Query strings "?" and same page anchors "#" wont work without the trailing slash preceding them for instance:

    yoursite.com/some-page/?querystring or yoursite.com/some-page/#anchor

    This is not too much of a big deal but I need these for statistics (from emails) and obviously same page anchors. They work like this but its just annoying, I have to add the page name first before the #


Any suggestions?
badlydrawnrob
 
Posts: 7
Joined: Wed Jul 23, 2008 8:45 am

Postby richardk » Fri Jul 25, 2008 3:49 pm

What URLs are you now trying to get (with or without trailing slashes)?
What happens when you try the mod_rewrite from my post?
What are the links in the source of the page like for anchors and query strings that don't work?
Where are you putting the mod_rewrite?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby badlydrawnrob » Sat Jul 26, 2008 10:43 am

Hey Richard...

I didnt try all of your post but tried the top part as the rest of it looked pretty much the same (by the way what is the difference between SCRIPT_REQUEST and REQUEST_FILENAME?)

I now decided to add trailing slash, the "add trailing slash" part didnt seem to do much with the rest of the code, but in one of your previous posts I found this gem:

Code: Select all
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+)/$ /$1.html [QSA,L]


And coupled it with previous add trailing slash etc as below:

Code: Select all
# Redirect 404 Error
ErrorDocument 404 /404.html

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(zeroplusdesign\.com)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://zeroplusdesign.com/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.+)$ /$1/ [L,R=301]

RewriteRule ^what-we-do/$ what-we-do.html [L]

RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+)/$ /$1.html [QSA,L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/$ /$1.php [QSA,L]

RewriteRule ^portfolio.html/$ http://zeroplusdesign.com/our-work/ [R=301,L]
RewriteRule ^packages.html/$ http://zeroplusdesign.com/what-we-do/ [R=301,L]


This solves all the problems besides the same page anchors which can be found here (I dont like giving the url of unfinished sites):

http://zeroplusdesign.com/what-we-do/#web

I am assuming this is because of the rewrite rule for extensions needs to be added after trailing slash as the slash is replacing .html?

The mod_rewrite is in the .htaccess

Thanks Richard you seem to be the main one helping round here!
badlydrawnrob
 
Posts: 7
Joined: Wed Jul 23, 2008 8:45 am

PS! you solved another problem

Postby badlydrawnrob » Sat Jul 26, 2008 12:37 pm

And another one bites the dust...

Blocking/redirecting original file extensions...

Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]


been looking for that bit of code everywhere, thought about letting robots.txt do the work but this solves it. Modified it a little from on of your posts...could I change this again to something like \(.html|.php) to catch all extensions?

I have a fair idea what it is doing but its bloody genious! Still no clue about the anchors like...

Lets hope the server doesnt blow up ;)
badlydrawnrob
 
Posts: 7
Joined: Wed Jul 23, 2008 8:45 am

Postby richardk » Sat Jul 26, 2008 3:12 pm

by the way what is the difference between SCRIPT_REQUEST and REQUEST_FILENAME?

SCRIPT_FILENAME and REQUEST_FILENAME are the same.

Modified it a little from on of your posts...could I change this again to something like \(.html|.php) to catch all extensions?

Yes.

This solves all the problems besides the same page anchors which can be found here (I dont like giving the url of unfinished sites):

http://exmaple.com/what-we-do/#web

I am assuming this is because of the rewrite rule for extensions needs to be added after trailing slash as the slash is replacing .html?

The anchor works fine. I don't understand the problem.

Code: Select all
# Redirect 404 Error
ErrorDocument 404 /404.html

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^portfolio\.html/$ http://zeroplusdesign.com/our-work/   [R=301,L]
RewriteRule ^packages\.html/$  http://zeroplusdesign.com/what-we-do/ [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://zeroplusdesign.com/ [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(zeroplusdesign\.com)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteRule ^what-we-do/$ what-we-do.html [L]

RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+)/$ /$1.html [QSA,L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/$ /$1.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby badlydrawnrob » Sun Jul 27, 2008 2:56 pm

Yeh the anchors work i just have to add the trailing slash then the anchor is all...

Do I have to add a backslash "\" before all periods "." ?

Thanks a lot Richard!!
badlydrawnrob
 
Posts: 7
Joined: Wed Jul 23, 2008 8:45 am

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests

cron