Solved

My new rewrite rule isnt working

Posted on 2006-11-28
23
362 Views
Last Modified: 2010-03-04
I'm trying to get to grips with these bloody rewrite rules :(

i have the following link now:
<a href="ContactUs/contact" title="Want To Contact Us?">Contact Us</a>

and the following rewrite rule:
RewriteRule ^ContactUs/([A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z])$ contactUs.php?mode=$1

I'm new to this, am i doing it write.

Basically what I want to happen is the mode of contact will be entered after ContactUs and then passed as an argument.

What am I doing wrong as it says the page can't be found?!

0
Comment
Question by:scm0sml
  • 13
  • 10
23 Comments
 

Author Comment

by:scm0sml
ID: 18030085
im trying to capture either the word contact or suggest, both 7 chars in length.
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18030208
What about?

RewriteEngine On
RewriteRule ^ContactUs/(contact|suggest)$ http://www.willowsv.com/contactUs.php?mode=$1 [r]
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18030213
You can ignore the domain part that was just me testing it was working.
0
 

Author Comment

by:scm0sml
ID: 18030256
ive got:
RewriteRule ^ContactUs/(contact|suggest)$ contactUs.php?mode=$1 [r]

im getting the same old:
The webpage cannot be found
 HTTP 404  
   Most likely causes:
There might be a typing error in the address.
If you clicked on a link, it may be out of date.
 
   What you can try:
     Retype the address.  
 
     Go back to the previous page.
 
     Go to 127.0.0.1 and look for the information you want.  
 
     More information

This error (HTTP 404 Not Found) means that Internet Explorer was able to connect to the website, but the page you wanted was not found. It's possible that the webpage is temporarily unavailable. Alternatively, the website might have changed or removed the webpage.

For more information about HTTP errors, see Help.
 
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18030270
Intresting it was working for me when I tried it from my local host to one of our web addresses.

the [r] flag specified a redirect what is the URL that ends up in the browser?

it should be http://www.mydomain.com/contactUs.php?mode=$1 if it is then the 404 is because for whatever reason its not finding that page.

0
 

Author Comment

by:scm0sml
ID: 18030302
0
 

Author Comment

by:scm0sml
ID: 18030335
i have a root directory where a.php is.

a.php includes a file which is in includes/footer.html

the link im using is in the html file.

the .htaccess file is in the root directory.

as the a.php renders the html at the root level this should work, right?
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18030403
Yeah that should be OK Apache is not concerned about PHP includes and these are ignored by any htaccess rules.

As long as the .htaccess is in the same folder as the PHP script all should be ok. For this purpose though it shouldent matter where the PHP file is as Apache works on incoming links. I could include a link to http://www.yourdomain.com/ContactUs/contact from my website and it would redirect the traffic. The only thing that would stop a rule from executing would be a previous matching rule with an [L] flag after it or the Rewrite Engine was not started.

Try as elimination purposes.

RewriteEngine On
RewriteRule ^ContactUs/(contact|suggest)$ http://127.0.0.1/contactUs.php?mode=$1 [r]

RewriteEngine On
RewriteRule ^*.ContactUs/(contact|suggest)$ contactUs.php?mode=$1 [r]
0
 

Author Comment

by:scm0sml
ID: 18030527
the first one didnt work and the 2nd threw an error
0
 

Author Comment

by:scm0sml
ID: 18030537
why is it not even redirecting then throwing an error when it realises it cant find the page, r we sure the rule is correct?
0
 

Author Comment

by:scm0sml
ID: 18030561
not a great time to ask this but i dont need to restart the server do i for the fresh .htaccess file to take effect?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:scm0sml
ID: 18030611
when it looks at ^ContactUs its looking for the directory, what am i doing wrong?
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18030649
No you don't need to restart the server after each pass with .htaccess.

maybe the issue is with the way its handling the URL in some way.

RewriteEngine On
RewriteRule ^/ContactUs/(contact|suggest)$ /contactUs.php?mode=$1 [r]

I can only assume this is down to Apache 1.3 / 2.x differences in the way it handles rewrites.

Just out of interest if the directory is causing problems does it Need to?

what I mean is something like this set-up.


RewriteEngine On
RewriteRule ^email_(contact|suggest)\.htm$ http://127.0.0.1/contactUs.php?mode=$1 [r]

then use email_contact.htm and email_suggest.htm in your links.

0
 

Author Comment

by:scm0sml
ID: 18030790
im very confused, it is working with:
RewriteRule ^contacts/(contact)/$ contactUs.php?mode=$1

and the link:
<a href="Contacts/contact/" title="Want To Contact Us?">Contact Us</a>

noting the non-capital 'c' for Contacts in the rewrite rule, does it not like capitals?

anyway this works.

but my style sheets are all  to pot, why is this?
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18033367
At a guess your style sheets are all to pot because your rewriting the URL and not redirecting ( You need to use the [r] flag to force a browser redirect otherwise that url is going to to keep the old url and execute and return the results of your PHP script. This will work with all links recursibly though all your subdomains to so that a request for say  say

/Contacts/contact/stylesheet.css is going to call and return the results of contactUs.php?mode=contact which is of course not a style sheet. If your not careful it may do the same with images.

As I said earlier yor regext enginge is doing funny things normally its case sensitive so doesent care but it may be you need to excape special characters or capitals with \ or / but not normally.
0
 

Author Comment

by:scm0sml
ID: 18034901
well the whole point of me doing these rewrites is to hide the original address......

a) as they are ugly
b) to help with search engines crawling my site.

so basically what you are saying is that i need another rule for my css stylesheets yeah?
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18035302
No you just need to tweak your rule slightly so it only handles .HTM .HTML files.

there's several ways you can do this

Place the Rewrite inside a

<Files ~ "\.(htm|html|php)$">

</Files>

Normally when rewriting for sites I just redirect say contact.htm to index.php?page=contact so only HTM pages are affected. Im not sure 100% how SEO works but its probably better to use contact/index.htm rather than just contact/

maybe change

RewriteRule ^contacts/(contact)/$ contactUs.php?mode=$1

to

RewriteRule ^contacts/(contact)/.*\.htm$ contactUs.php?mode=$1 so it will only redirect the request is a .htm file is requested and not say a css.

Or add a RewriteCond

RewriteCond %{REQUEST_URI } ^.*\.(htm|html|php)$
RewriteRule ^contacts/(contact)/$ contactUs.php?mode=$1

You will have to have a play to see which way works best for you.
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18035307
The other way of course is to get it to Ignore CSS/JPG requests etc.

RewriteCond %{REQUEST_URI } !^.*\.(css|jpe?g|gif|png)$
RewriteRule ^contacts/(contact)/$ contactUs.php?mode=$1
0
 

Author Comment

by:scm0sml
ID: 18035392
im slightly confused by all that to be honest.

the css style sheet is referenced from the php page that gets called by the rewrite rule, but in the address bar the 'front' value is displayed, is it not using my css because it is trying to use the path to the stylesheet from the Conatct/.....address rather than the php?
0
 
LVL 13

Accepted Solution

by:
rhickmott earned 500 total points
ID: 18035481
Yes if you rewrite say

http://www.mydomain.com/contact/us/contact.htm

to

http://www.mydomain.com/mypage.php?action=contact.

Then any relative urls called from that page ie stylesheet.css will try and call

http://www.mydomain.com/contact/us/stylesheet.css

This is the problem with URL rewriting when HIDING the original URLs the browser doesent know that its hidden so will try and grab files from the same directory it thinks its getting them from.

you will need to use full paths in images and external files ie http://www.mydomain.com/stylesheet.css which is ugly or get the rewrite rule to rewrite

say contact/us/contact.htm to http://www.mydomain.com/index.php?action=contact and then all requests for files to the correct folder.

so

RewriteEngine On
RewriteRule ^contactus/(contact)/.*\.(HTM?L|PHP)$ /contactUs.php?mode=$1 [L]
RewriteRule ^contactus/(contact)/$ /contactUs.php?mode=$1 [L]
RewriteRule ^contactus/(contact)/.*$ /$1

The [L] Flag at the end means if it mates this rule don't bother processing others. So if its a request for .htm .php or .html is should do the first rule if its something else it will do the second rule.

so

http://www.domain.com/contactus/contact/index.htm should pull http://www.domain.com/contactUs.php?mod=contact
http://www.domain.com/contactus/contact/ should pull http://www.domain.com/contactUs.php?mod=contact
http://www.domain.com/contactus/contact/sheet.css should pull http://www.domain.com/sheet.css

The problem is of course with using Ls is if you have another block of rules doing something else it can stop them executing so you have to be careful.
0
 

Author Comment

by:scm0sml
ID: 18039038
i've got a work around with this!!

<a href="contact.htm" title="Want To Contact Us?">Contact Us</a>

so in the root folder

with the rule:
RewriteRule ^(contact).htm$ contactUs.php?mode=$1

then my style sheets are referenced from the right level!!

hoorah!
0
 

Author Comment

by:scm0sml
ID: 18039049
i think we've gotten this answer together so thanks!!
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18041635
Glad to see you have it sorted :)

Rewrites can be such a pain sometimes.

Regards
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now