Forum Moderators: phranque

Message Too Old, No Replies

URL Rewrite .htaccess not working as expected ?

         

mat24

5:07 pm on Apr 13, 2012 (gmt 0)

10+ Year Member



Hi,

I have a php signup page at www.mydomain.com/signup.php

Using .htaccess i am trying to make it so it shows like this in the browser www.mydomain.com/signup/

Even thou i can get it to work partially it's not working as i would like it to.

Basically if i visit link like: www.mydomain.com/signup/ it works

if i visit link without trailing slash it works in FireFox but not in Chrome or IE, it just basically has problems loading the page.

if i visit the url as www.mydomain.com/signup.php i can still access it, i would like it so someone can only access via the friendly url and not the actual link as shown just above.

Another problem i have if i visit the page in particular like www.mydomain.com/signup/ and submit the form on the page it keeps adding /signup.php/ and repeats that on the url.

Here is the url rewrite.

RewriteRule ^signup/(.*)$ signup.php [NC]


I would like it so if the user did or did not enter a forward slash on the end it will still be a valid page and want to try and not allow access directly like www.mydomain.com/signup.php, (basically only want them to access it via friendly url.

Would appreciate it if someone could help solve my url rewrite, i am not very good with regex and url rewriting.

Thank you
Mat

g1smd

2:11 am on Apr 14, 2012 (gmt 0)

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



You do not want the [NC] flag. The NC flag allows duplicate any-cased URLs to also be valid.

URL ending with slash denotes a folder. You need URL without trailing slash to be valid.

You should be using the URL example.com/signup here:

RewriteRule ^signup$ /signup.php [L]


Rule order is important. This rewrite should be listed after all of the redirects.

The redirect will work in the other direction but requires a preceding RewriteCond looking at THE_REQUEST so that only external requests for signup.php (and not as a result of a previous rewrite) will be redirected. This avoids an infinite loop.

Having a URL both with and without trailing slash as valid is duplicate content. Redirect "with slash" to "without".

mat24

10:06 am on Apr 14, 2012 (gmt 0)

10+ Year Member



Hi,

Thanks for replying, much appreciated.

Yours works so thanks also understand more now after a good explantion from you but i am still able to access it like wwww.mydomain.com/signup.php

i only want it accessible like www.mydomain.com/signup

is it possible ?

here is my .htaccess file, maybe i have mixed it up, i have replaced all instances of my real domain name with mydomain


RewriteEngine on
# Options -Indexes

# Prevent Indexing
IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

# force www. in urls
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

RewriteCond %{HTTP_REFERER} !^http://mydomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://mydomain.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.mydomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.mydomain.com$ [NC]
RewriteRule .*\.(.[^/]*jpg|jpeg|gif|png|bmp|psd)$ - [F,NC]

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>

<FilesMatch "\.(ht|htaccess|htpasswd|ini|log|inc|error_log)$">
Order Allow,Deny
Deny from all
</FilesMatch>

# CACHE CONTROLS
#
# Set expiration header
# 2419200 - 4 week | 29030400 - 1 year
# ExpiresActive on
# ExpiresByType image/gif A29030400
# ExpiresByType image/png A29030400
# ExpiresByType image/jpeg A29030400
# ExpiresByType image/jpg A29030400
# ExpiresByType text/css A2419200
# ExpiresByType text/javascript A2419200
# ExpiresByType text/js A2419200

# Compress some text file types
# <FilesMatch "\.(php|html|css|js)$">
# SetOutputFilter DEFLATE
# </FilesMatch>
# AddOutputFilterByType DEFLATE text/html text/css text/xml application/xhtml+xml application/rss+xml application/x-javascript text/javascript text/js

# URL Changes - Permanent Redirects
# Redirect 301 /contact/contactus.php http://www.mydomain.com/contactus.php

# Custom Error Documents
ErrorDocument 404 /error/404.php
ErrorDocument 400 /error/400.php
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 500 /error/500.php
ErrorDocument 501 /error/501.php
ErrorDocument 502 /error/502.php
ErrorDocument 503 /error/503.php


# BAD BOT
RewriteCond %{HTTP_USER_AGENT} ^BlackWidow [OR]
RewriteCond %{HTTP_USER_AGENT} ^Bot\ mailto:craftbot@yahoo.com [OR]
RewriteCond %{HTTP_USER_AGENT} ^ChinaClaw [OR]
RewriteCond %{HTTP_USER_AGENT} ^Custo [OR]
RewriteCond %{HTTP_USER_AGENT} ^DISCo [OR]
RewriteCond %{HTTP_USER_AGENT} ^Download\ Demon [OR]
RewriteCond %{HTTP_USER_AGENT} ^eCatch [OR]
RewriteCond %{HTTP_USER_AGENT} ^EirGrabber [OR]
RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} ^EmailWolf [OR]
RewriteCond %{HTTP_USER_AGENT} ^Express\ WebPictures [OR]
RewriteCond %{HTTP_USER_AGENT} ^ExtractorPro [OR]
RewriteCond %{HTTP_USER_AGENT} ^EyeNetIE [OR]
RewriteCond %{HTTP_USER_AGENT} ^FlashGet [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetRight [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetWeb! [OR]
RewriteCond %{HTTP_USER_AGENT} ^Go!Zilla [OR]
RewriteCond %{HTTP_USER_AGENT} ^Go-Ahead-Got-It [OR]
RewriteCond %{HTTP_USER_AGENT} ^GrabNet [OR]
RewriteCond %{HTTP_USER_AGENT} ^Grafula [OR]
RewriteCond %{HTTP_USER_AGENT} ^HMView [OR]
RewriteCond %{HTTP_USER_AGENT} HTTrack [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Image\ Stripper [OR]
RewriteCond %{HTTP_USER_AGENT} ^Image\ Sucker [OR]
RewriteCond %{HTTP_USER_AGENT} Indy\ Library [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^InterGET [OR]
RewriteCond %{HTTP_USER_AGENT} ^Internet\ Ninja [OR]
RewriteCond %{HTTP_USER_AGENT} ^JetCar [OR]
RewriteCond %{HTTP_USER_AGENT} ^JOC\ Web\ Spider [OR]
RewriteCond %{HTTP_USER_AGENT} ^larbin [OR]
RewriteCond %{HTTP_USER_AGENT} ^LeechFTP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mass\ Downloader [OR]
RewriteCond %{HTTP_USER_AGENT} ^MIDown\ tool [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mister\ PiX [OR]
RewriteCond %{HTTP_USER_AGENT} ^Navroad [OR]
RewriteCond %{HTTP_USER_AGENT} ^NearSite [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetAnts [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Net\ Vampire [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetZIP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Octopus [OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline\ Explorer [OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline\ Navigator [OR]
RewriteCond %{HTTP_USER_AGENT} ^PageGrabber [OR]
RewriteCond %{HTTP_USER_AGENT} ^Papa\ Foto [OR]
RewriteCond %{HTTP_USER_AGENT} ^pavuk [OR]
RewriteCond %{HTTP_USER_AGENT} ^pcBrowser [OR]
RewriteCond %{HTTP_USER_AGENT} ^RealDownload [OR]
RewriteCond %{HTTP_USER_AGENT} ^ReGet [OR]
RewriteCond %{HTTP_USER_AGENT} ^SiteSnagger [OR]
RewriteCond %{HTTP_USER_AGENT} ^SmartDownload [OR]
RewriteCond %{HTTP_USER_AGENT} ^SuperBot [OR]
RewriteCond %{HTTP_USER_AGENT} ^SuperHTTP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Surfbot [OR]
RewriteCond %{HTTP_USER_AGENT} ^tAkeOut [OR]
RewriteCond %{HTTP_USER_AGENT} ^Teleport\ Pro [OR]
RewriteCond %{HTTP_USER_AGENT} ^VoidEYE [OR]
RewriteCond %{HTTP_USER_AGENT} ^Web\ Image\ Collector [OR]
RewriteCond %{HTTP_USER_AGENT} ^Web\ Sucker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebAuto [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebCopier [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebFetch [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebGo\ IS [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebLeacher [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebReaper [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebSauger [OR]
RewriteCond %{HTTP_USER_AGENT} ^Website\ eXtractor [OR]
RewriteCond %{HTTP_USER_AGENT} ^Website\ Quester [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebStripper [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebWhacker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebZIP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Wget [OR]
RewriteCond %{HTTP_USER_AGENT} ^Widow [OR]
RewriteCond %{HTTP_USER_AGENT} ^WWWOFFLE [OR]
RewriteCond %{HTTP_USER_AGENT} ^Xaldon\ WebSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Zeus
RewriteRule ^.* - [F,L]
# BAD BOT END

# Pretty URLS
RewriteRule ^signup$ /signup.php [L]
RewriteRule ^login$ /login.php [L]


# SPAMMY IPs BLOCK
deny from 92.241.169.70
deny from 91.201.66.104
deny from 79.142.79.10
# SPAMMY IPs BLOCK END


Thank you for any assistance!
Mathew

g1smd

10:25 am on Apr 14, 2012 (gmt 0)

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



delete

[edited by: g1smd at 10:36 am (utc) on Apr 14, 2012]

g1smd

10:30 am on Apr 14, 2012 (gmt 0)

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



So that only one URL works, you need to add a redirect from example.com/signup.php to example.com/signup

The redirect will work in the other direction to your rewrite but it requires a preceding RewriteCond looking at THE_REQUEST so that only external requests for signup.php (and not as a result of a previous rewrite) will be redirected. This avoids an infinite loop.

THE_REQUEST contains the literal
GET /signup.php HTTP/1.1
request issued by the browser.


In your example code above you should move your bad bod code so that it is before all of the other RewriteRule code i.e. just after the RewriteEngine On directive.

Move your <Limit> (two), <Filesmatch> (two) and "deny" directives so they at the very beginning of the file before the RewriteEngine On directive.


Make sure that every RewriteRule has the L flag. You missed at least one.


Convert your Redirect 301 line to instead use RewriteRule syntax and move it so it is before the 'force www' ruleset.


In your 'force www' RewriteCond change the pattern to
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$


Escape all literal periods in RegEx patterns.


Use 'example.com' in ths forum. This suppresses the auto-linking function.

wilderness

11:57 am on Apr 14, 2012 (gmt 0)

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



your copy and pasted BAD BOT list is more than a decade old.
For all the good it does (doesn't) do, you may as well remove the entire section.

mat24

12:34 pm on Apr 14, 2012 (gmt 0)

10+ Year Member



Thanks g1smd,

think i got it rite now.

Thanks
Mathew