Forum Moderators: phranque

Message Too Old, No Replies

Htaccess redirect 404 url problem

404 redirect

         

danelsasser

5:39 pm on Jul 8, 2011 (gmt 0)

10+ Year Member



Hi everyone, any help would be greatly appreciated.

I'm have a problem with invalid urls not hitting a 404 page but instead creating pages with template but no usefull info.

For example:
[jailnation.com ]

Now when i add random letters in there:
[jailnation.com ]

I would like to redirect all these types of pages to the siteroot of my website [jailnation.com...]

Current htaccess:

RewriteCond %{HTTP_HOST} ^jailnation.com [NC]
RewriteRule ^(.*)$ http://www.jailnation.com/$1 [L,R=301]
RewriteRule ^index.php$ http://www.jailnation.com/ [R=301,QSA]
RewriteRule blog/$ blog/index.php
RewriteRule cms/$ cms/index.php

#RewriteRule ^nh/$ jail_state.php?state_url=nvt
RewriteCond %{REQUEST_URI} !^blog
#RewriteRule ([^.php]*)/$ jail_state.php?state_url=$1%{QUERY_STRING}

RewriteCond %{REQUEST_URI} !^blog/wp-admin/
RewriteRule blog/wp-admin/$ blog/wp-admin/index.php


RewriteRule ^([^/]*)/([^/]*)/$ jail_description.php?state_url=$1&county_url=$2 [QSA]

RewriteRule ^CompanyProfile/([^/]*)$ company_details.php$1 [L]
RewriteRule ^Login$ login.php [L]
RewriteRule ^MyAccount$ company_details.php [L]
RewriteRule ^EditProfile$ edit_profile.php [L]
RewriteRule ^ChangePassword$ change_password.php [L]
RewriteRule ^Logout$ logout.php [L]
RewriteRule ^JailBailBondsListing$ jail_bailbonds_paid_listing.php [L]
RewriteRule ^JailAttorneyListing$ jail_attorney_paid_listing.php [L]
RewriteRule ^BrowseLegalFAQ$ browse_legal_faq.php [L]
RewriteRule ^HowToHelp$ how-to-help.php [L]
RewriteRule ^ContactUs$ contact_us.php [L]
RewriteRule ^OurMission$ our_mission.php [L]
RewriteRule ^SiteMap$ sitemap.php [L]
RewriteRule ^AdvertisingResorces$ advertising_resorces.php [L]
RewriteRule ^BrowseBailFAQ$ browse_bail_faq.php [L]

RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]*)/$ jail_state.php?state_url=$1%{QUERY_STRING}
#ErrorDocument 404 http://jailnation.com/error/?do=error404
#ErrorDocument 404 /404.php
</IfModule>

danelsasser

5:40 pm on Jul 8, 2011 (gmt 0)

10+ Year Member



RewriteRule ^([^/]*)/([^/]*)/$ jail_description.php?state_url=$1&county_url=$2 [QSA]

g1smd

5:58 pm on Jul 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Many of those rules have syntax errors and they are certainly in the wrong order. The index redirect MUST be before the non-www redirect.

Literal periods in patterns must be escaped.

Every rule needs the [L] flag added.

[^.php]*)
is a negative match character group. As such it does not match any request with a period in or which contains either a p or an h.

^([^/]*)/([^/]*)/$
allows a request for
example.com///
to be valid. Use + not * here.

Add a blank line after every RewriteRule to make the code easier to read.

Don't reappend the {QUERY_STRING} variable, use the [QSA] flag. You did that on some rules but not others.

danelsasser

6:59 pm on Jul 8, 2011 (gmt 0)

10+ Year Member



Thank you G1 for your help. I am still having trouble when adding a random set of letters to a directory in the url it is creating the page and displaying it.

Good: htp://www.jailnation.com/ny/green-county/
Should be 404: htp://www.jailnation.com/ny/green-county-asdasdasdasdasdasdasdasdasd/
Should be 404: htp://www.jailnation.com/nyasdasdasdasdasdasdasdasdasd/green-county-/



Here is my revised htaccess now:

<IfModule mod_rewrite.c>

Options +FollowSymLinks
#Options +SymLinksIfOwnerMatch
RewriteEngine on

RewriteRule ^index.php$ [jailnation.com...] [R=301,QSA]

RewriteCond %{HTTP_HOST} ^jailnation.com [NC]
RewriteRule ^(.*)$ [jailnation.com...] [L,R=301]


RewriteRule blog/$ blog/index.php [L]
RewriteRule cms/$ cms/index.php [L]

#RewriteRule ^nh/$ jail_state.php?state_url=nvt [L]

RewriteCond %{REQUEST_URI} !^blog
#RewriteRule ([^.php]*)/$ jail_state.php?state_url=$1%{QUERY_STRING} [QSA]

RewriteCond %{REQUEST_URI} !^blog/wp-admin/
RewriteRule blog/wp-admin/$ blog/wp-admin/index.php [L]


RewriteRule ^([^/]+)/([^/]+)/$ jail_description.php?state_url=$1&county_url=$2 [QSA]

RewriteRule ^CompanyProfile/([^/]*)$ company_details.php$1 [L]

RewriteRule ^Login$ login.php [L]

RewriteRule ^MyAccount$ company_details.php [L]

RewriteRule ^EditProfile$ edit_profile.php [L]

RewriteRule ^ChangePassword$ change_password.php [L]

RewriteRule ^Logout$ logout.php [L]

RewriteRule ^JailBailBondsListing$ jail_bailbonds_paid_listing.php [L]

RewriteRule ^JailAttorneyListing$ jail_attorney_paid_listing.php [L]

RewriteRule ^BrowseLegalFAQ$ browse_legal_faq.php [L]

RewriteRule ^HowToHelp$ how-to-help.php [L]

RewriteRule ^ContactUs$ contact_us.php [L]

RewriteRule ^OurMission$ our_mission.php [L]

RewriteRule ^SiteMap$ sitemap.php [L]

RewriteRule ^AdvertisingResorces$ advertising_resorces.php [L]

RewriteRule ^BrowseBailFAQ$ browse_bail_faq.php [L]

RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+)/$ jail_state.php?state_url=$1%{QUERY_STRING} [QSA]

ErrorDocument 404 [jailnation.com...]
ErrorDocument 404 /404.php
</IfModule>

g1smd

7:16 pm on Jul 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Add the [L] flag to every every every every rule.

Escape literal periods in all RegEx patterns.

RewriteRule blog/$ blog/index.php [L]
and others can be served by using
DirectoryIndex index.php


You might want to check the logic here:
RewriteCond %{REQUEST_URI} !^blog/wp-admin/
RewriteRule blog/wp-admin/$ blog/wp-admin/index.php [L]

as the rule can never work. When the RegEx pattern is true the RewriteCond is false and vice versa.

([^.php]*)/$
does not do what you think it does and is incorrect.

RewriteRule ^CompanyProfile/([^/]*)$ company_details.php$1 [L]

can never work because the PREVIOUS rule already captured the request as it uses the more general
^([^/]+)/([^/]+)/$
pattern.
Additionally, once you fix that you'll run into a different problem. A request for
example.com/CompanyProfile/SomeVar
will be written to the internal filepath at
/company_details.phpSomeVar
which will obviously 404.

lucy24

8:15 pm on Jul 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am still having trouble when adding a random set of letters to a directory in the url it is creating the page and displaying it.

This is not an .htaccess problem. .htaccess cannot create files where none existed before. It lies elsewhere in your site-generating code. (wikis for example behave the same way: type in any old garbage and they will give you the option of creating a page with this name.) The most you can do in .htaccess is check whether the requested file already exists using -f in the RewriteCond. And this will only work if the request hits .htaccess before it hits whatever it is that's creating the page.

danelsasser

12:05 pm on Jul 9, 2011 (gmt 0)

10+ Year Member



Very much appreciated Lucy and G1.

danelsasser

11:09 pm on Jul 13, 2011 (gmt 0)

10+ Year Member



I've solved the problem, thanks. I figured i would post the htaccess file in case you saw anything else. Thanks for you help!


ErrorDocument 404 /404.php
# BEGIN Gzip
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</IfModule>
# END Gzip

# 480 weeks

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|cur)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>

# 2 DAYS

<FilesMatch "\.(xml|txt)$">
Header set Cache-Control "max-age=172800, public, must-revalidate"
</FilesMatch>

# 2 HOURS

<FilesMatch "\.(html|htm)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>

<FilesMatch "image.php$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>

<Files [jailnation.com...]
SetOutputFilter DEFLATE
</Files>

<IfModule mod_rewrite.c>

Options +FollowSymLinks
#Options +SymLinksIfOwnerMatch
RewriteEngine on

RewriteRule ^index.php$ [jailnation.com...] [R=301,QSA]

RewriteCond %{HTTP_HOST} ^jailnation.com [NC]
RewriteRule ^(.*)$ [jailnation.com...] [L,R=301]


RewriteRule blog/$ blog/index.php [L]
RewriteRule cms/$ cms/index.php [L]

#RewriteRule ^nh/$ jail_state.php?state_url=nvt [L]

RewriteCond %{REQUEST_URI} !^blog
#RewriteRule ([^.php]*)/$ jail_state.php?state_url=$1 [QSA]

RewriteCond %{REQUEST_URI} !^blog/wp-admin/
RewriteRule blog/wp-admin/$ blog/wp-admin/index.php [L]


RewriteRule ^([^/]+)/([^/]+)/$ jail_description.php?state_url=$1&county_url=$2 [QSA]

RewriteRule ^CompanyProfile/([^/]*)$ company_details.php$1 [L]

RewriteRule ^Login$ login.php [L]

RewriteRule ^MyAccount$ company_details.php [L]

RewriteRule ^EditProfile$ edit_profile.php [L]

RewriteRule ^ChangePassword$ change_password.php [L]

RewriteRule ^Logout$ logout.php [L]

RewriteRule ^JailBailBondsListing$ jail_bailbonds_paid_listing.php [L]

RewriteRule ^JailAttorneyListing$ jail_attorney_paid_listing.php [L]

RewriteRule ^BrowseLegalFAQ$ browse_legal_faq.php [L]

RewriteRule ^HowToHelp$ how-to-help.php [L]

RewriteRule ^ContactUs$ contact_us.php [L]

RewriteRule ^OurMission$ our_mission.php [L]

RewriteRule ^SiteMap$ sitemap.php [L]

RewriteRule ^AdvertisingResorces$ advertising_resorces.php [L]

RewriteRule ^BrowseBailFAQ$ browse_bail_faq.php [L]

RewriteRule ^SubmitMoreInfo$ submit_more_info.php [L]

RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+)/$ jail_state.php?state_url=$1 [QSA]

#ErrorDocument 404 [jailnation.com...]
#ErrorDocument 404 /404.php
</IfModule>

g1smd

11:47 pm on Jul 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Escape literal periods in patterns.

Add the [L] flag to every rule.

RewriteRule blog/$ blog/index.php [L] and others can be served by using DirectoryIndex index.php

([^.php]*)/$ does not do what you think it does and is incorrect.

You might want to check the logic here:
RewriteCond %{REQUEST_URI} !^blog/wp-admin/
RewriteRule blog/wp-admin/$ blog/wp-admin/index.php [L]
as the rule can never work. When the RegEx pattern is true the RewriteCond is false and vice versa.

RewriteRule ^CompanyProfile/([^/]*)$ company_details.php$1 [L]
can never work because the PREVIOUS rule already captured the request as it uses the more general ^([^/]+)/([^/]+)/$ pattern.
Additionally, once you fix that you'll run into a different problem. A request for example.com/CompanyProfile/SomeVar will be written to the internal filepath at /company_details.phpSomeVar which will obviously 404.