Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
A donation makes a contribution towards the costs, the time and effort that's going in this site and building.
Thank You! Steffen
Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
| |
|
Topic: Would appreciate help to write a .htaccess file for a 301 |
|
Author |
|
Ghostwise
Joined: 15 Apr 2013 Posts: 5 Location: Brasil
|
Posted: Mon 15 Apr '13 22:22 Post subject: Would appreciate help to write a .htaccess file for a 301 |
|
|
Hello folks,
I am a less-than-technical person, and though I have learned some bits of regular expressions for simple scripting, writing a .htaccess file is, uh, syntaxically daunting.
THE CASE :
The URLs of my site used to be of the form http://www.writeups.org/affiche_fiche.php?id=1234 . They are now of the form http://www.writeups.org/fiche.php?id=1234 .
I am trying to perma-redirect (301) the old format (affiche_fiche.php) to the new format (fiche.php) using a .htaccess.
So far all I have achieved is a hatred of punctuation signs. What's the correct syntax to have a .htaccess that does the redirect ?
THE CONTEXT :
The format change took place more than six months ago, but the Google Webmaster Tools still spits 450 problems a day with 404s on URLs using the old format. I had assumed that these would just fade away, but they don't. So I guess that 301'ing them is cleaner. Or would be, if I understood the syntax.
Thanks y'all. |
|
Back to top |
|
Steffen Moderator
Joined: 15 Oct 2005 Posts: 3118 Location: Hilversum, NL, EU
|
Posted: Tue 16 Apr '13 10:49 Post subject: |
|
|
Please try: Code: | RewriteRule ^/affiche_fiche\.php(.*) http://www.writeups.org/fiche\.php$1 [L,R=301] |
Steffen |
|
Back to top |
|
Ghostwise
Joined: 15 Apr 2013 Posts: 5 Location: Brasil
|
Posted: Tue 16 Apr '13 17:53 Post subject: |
|
|
Hello Steffen,
It doesn't work right away, so of course I tried haphazard variations.
Content of the .htaccess in my htdocs folder:
Code: |
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/affiche_fiche\.php(.*)$ http://www.writeups.org/fiche\.php$1 [R=301,L]
ErrorDocument 404 /content/404.html
|
What I think I'm doing :
- the first two lines are TMK the boilerplate for a rewrite rule in a htaccess
- then, we state the rewrite rule
- the first string is what we expect to encounter ; it starts with a ^ and ends with $. The \ is presumably an escape character so the . isn't treated as a meta-thingie.
- the ".*" notation in the first string means "any series of non-space characters after the string that is written out".
- the second string is the full url in the new format, and the $1 at the end means "paste here the parenthesised part from the first string, i.e. everything that was after 'php' in the first string".
- [R=301,L] means "signal a 301 (permanent redirect) then leave without applying further rules"
I'm not sure why you kept a \ in the second string, but removing it doesn't change anything.
What it actually does :
- doesn't get in the way of serving the normal URLs. That's good.
- correctly redirects to a custom 404 page. Good, and it shows that the .htaccess is interpreted.
- doesn't apply the rewrite rule. So for instance http://www.writeups.org/affiche_fiche.php?id=5500 currently 404s cleanly - but doesn't 301 to http://www.writeups.org/fiche.php?id=5500 as it should. The URL is unchanged in the browser.
I'm probably missing some annoying detail, since from all I've been reading the syntax smells correct. Any idea ? |
|
Back to top |
|
Steffen Moderator
Joined: 15 Oct 2005 Posts: 3118 Location: Hilversum, NL, EU
|
Posted: Tue 16 Apr '13 18:07 Post subject: |
|
|
I applied my rule above in my httpd.conf, it redirects to your site.
I see you placed a $ at the end of the first part. Why ?
Steffen |
|
Back to top |
|
Ghostwise
Joined: 15 Apr 2013 Posts: 5 Location: Brasil
|
Posted: Tue 16 Apr '13 21:45 Post subject: |
|
|
Steffen wrote: | I see you placed a $ at the end of the first part. Why ? |
Because I read that it was supposed to be used to close the first string (though I almost never saw it in the tutorials I've read), and I tried various tweaks when the first attempt didn't work.
Next attempt - copy-pasted your suggestion back in, to delete the $ and any other deviation my tests may have caused. Same behaviour. Current content of the file :
Code: |
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/affiche_fiche\.php(.*) http://www.writeups.org/fiche\.php$1 [L,R=301]
ErrorDocument 404 /content/404.html
|
Next attempt - tried with one space instead of two between the two strings, same behaviour.
Next attempt - moved up the .htaccess file from the htdocs folder to the root folder for the website. Same behaviour.
So if I sum it up :
- the syntax works since you tested it on your end
- the redirection toward my 404 works, so I didn't make a weird beginner mistake as to where I placed the file
In which direction should I dig next, in your opinion ? |
|
Back to top |
|
Steffen Moderator
Joined: 15 Oct 2005 Posts: 3118 Location: Hilversum, NL, EU
|
Posted: Tue 16 Apr '13 21:58 Post subject: |
|
|
Do you have other rewrite rules active ?
Using Virtual hosts ?
Try to place it in httpd.conf instead of .htaccess
Steffen |
|
Back to top |
|
Ghostwise
Joined: 15 Apr 2013 Posts: 5 Location: Brasil
|
Posted: Thu 18 Apr '13 13:57 Post subject: |
|
|
No rewrites. I will read about the other subjects and see what applies to my case, thank you. |
|
Back to top |
|
Ghostwise
Joined: 15 Apr 2013 Posts: 5 Location: Brasil
|
Posted: Sat 27 Apr '13 18:54 Post subject: |
|
|
Hello folks,
I was delayed by some work stuff, my apologies.
After further reading and experimentation, my .htaccess ended up in my htdocs folder and now reads :
RedirectMatch /affiche_(.*)$ /$1
ErrorDocument 404 /content/404.html
This successfully strips the string "affiche_" from the URL, leaving it to conform to the new format. And it continues to handle 404s as before.
RedirectMatch is found in something called mod_alias, but don't ask me what it is or how I found it.
Are there side effects I should be aware of, or is it a decent solution ?
Thanks. |
|
Back to top |
|
|
|
|
|
|