Making redirections .. need your help guys!

Hi …

I need to make some redirections, I’m changing the script used in my website, and I need toclean some URLs as well …

First,

I need to make a permanent redirection for about 300 pages (articles) …

The old URLs are like

with the new script installed , I would like all the URLs in that format to be redirected to

Second,

I need to make a permanent redirection …

the old URLs are like:
www.example.com/page-title.php

I would like to rewrite these URLs to:
www.example.com/page-title

also, I would like to make a permanent redirection for all the old URLs to the new ones after making rewrite thing, because I don’t want to lose all the backlinking done …

the two manipulation are for the same website …

my .htaccess file looks like

# -FrontPage-

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

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName mysite.com
AuthUserFile /home/mysite/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/mysite/public_html/_vti_pvt/service.grp




Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]


suPHP_ConfigPath /home/mysite/public_html/php.ini

what can I add ?? will be extremely thankful …

Best Regards.

eLaw,

What have you tried with relation to your two “problems”? All your .htaccess is doing is limiting access to the website then enforcing www in the domain name.

HINT: Look for .htm and .php file extensions and redirect without them. Put that before your CMS mod_rewrite so the extensionless URIs can then be handled by the CMS.

Regards,

DK

Hi …

yes I know, the code given above is the actual .htaccess file (I think that you gave me the code to redirect the www if I remember well :slight_smile: that was 1 or two years ago !!) … I’m giving the code because what I tried (after seing tutorials etc) gave me 500 internal server error … so I thought there were some interferences with my existing code …

I thought that someone may help me … to be honest I don’t know anything about apache … just following tutorials I found online …

can you help me please ?:slight_smile:

will be really thankful …

PS: to be honest, I looked for help in several forums and I didn’t find a solution … and I remembered that the working actual code was given by one of sitepoint members (I didn’t visit this forum for like a year , maybe more) … and I’m pretty sure that member was you ! so in short, I just came here so you can help me with this issue !

PS2: I can post here the codes I tried if you want …

Thanks

Yes, please.

Regards,

DK

ok,

I actually tried two codes, and the two are related to the second “problem”

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /(.*).php HTTP/ [NC]
RewriteRule .+ http://www.yourwebsite.com/%1  [R=301,QSA]

and

<IfModule mod_rewrite.c>
RewriteEngine on


# index.php to /
RewriteCond %{THE_REQUEST} ^GET /.*/index.(php|html) HTTP
RewriteRule (.*)index.(php|html)$ /$1 [R=301,L]

# force www.
rewritecond %{HTTP_HOST} ^somesite.com [nc]
rewriterule ^(.*)$ http://www.somesite.com/$1 [r=301,nc]
</IfModule>

added after my current code >>> internal server error …

for the first problem, I got 2 codes, but I din’t try them since I’m still stuck with the second issue …

RewriteEngine on
RewriteBase /
RewriteRule ^example-1/([0-9A-Za-z\\-]+).htm$ example-2/$1 [R=301,L,QSA]

and

RewriteEngine on
RewriteBase /
RewriteRule ^example-1/(.+).htm$ example-2/$1 [R=301,L,QSA]

Regards,

DK

Thanks …

If I just add

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /(.*).php HTTP/ [NC]
RewriteRule .+ http://www.yourwebsite.com/%1  [R=301,QSA]

after the current code … I mean if this is my code in the .htaccess file



# -FrontPage-

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

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName youtips.net
AuthUserFile /home/youtips/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/youtips/public_html/_vti_pvt/service.grp



Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.com [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /(.*).php HTTP/ [NC]
RewriteRule .+ http://www.site.com/%1  [R=301,QSA]

I got an internal server error …

Great ! I’m not sure that I’m following :smiley:

can you explain more ? perhaps give an example ?

as I told you before, I just know xHTML/css and php … just messing up .htaccess files :rolleyes:

eLaw,

Correct … as it should be!

Regards,

DK

Regards,

DK

Okay I see …

I’m speaking about the second "problem "

after using the code, all the URLs ending with .php were changed … but when visiting these pages I got an error (page not found) …

I don’t know if I explained my problem well, because it seems that you corrected the codes people gave me … and I think these codes aren’t the solution …

for the second problem I don’t need a redirection … I have .php files in the main directory, that I can access using the URL : www.site.com/page-one.php

I just want these same pages to be accessed when visiting www.site.com/page-one

it’s rewrite not a redirection as it seems … I don’t really understand these things sorry …

when I used

RewriteCond %{THE_REQUEST} [1]{3,9}\ /(.*).php\ HTTP/ [NC]
RewriteRule .+ http://www.site.com/%1 [R=301,QSA]

the URLs were redirected (no internal server error this time!) … it means if I visit www.site.com/page-one.php it redirects me to www.site.com/page-one (which is good) but I got “page not found” …

for the first problem , I don’t need to rewrite the URLs; because all the new URLs exist already, I just need to redirect all the www.site.com/example-1/article-title.htm to the new location www.site.com/example-2/article-title
(the purpose is to not lose all the backlinks already pointing to these articles)

I used

RewriteEngine on
RewriteBase /
RewriteRule ^example-1/(.+).htm$ example-2/$1 [R=301,L,QSA]

but when I access an old article at the old URL www.site.com/example-1/article-title.htm nothing happened … no redirection … nothing …


  1. A-Z ↩︎

the URLs were redirected (no internal server error this time!) … it means if I visit www.site.com/page-one.php it redirects me to www.site.com/page-one (which is good)

No, it’s not!

but I got “page not found” …

because the resource “yoursite.com/page-one” does not actually exist.

You DON’T want www.yoursite.com/page-one.php to get redirected! What you want, a rewrite, is that IF someone types in
www.yoursite.com/page-one.php
then that’s what they get like before (sorry, however if you change all the hyperlinks in your pages and ads etc then they won’t be going here very often)
BUT
if someone types in (or clicks a link)
www.yoursite.com/page-one
that the user does not see a new url (they see the page-one without the .php like you want), but DOES see the content at
www.yoursite.com/page-one.php
(because this is the real page who actually exists in your system)

So what a rewrite (not redirect) does is make other, prettier URLs actually bring back the real, ugly URL’s resource (page).

it’s rewrite not a redirection as it seems …
Yup, you got it.

So all you want to do for those URLs is something like (example! all mistakes are mine!):

RewriteRule ^([-\w\d]+)(\.php)?$ $1.php [L]

[1]+ is an example representing anything (at least one character of letters, digits or hyphens anyway) after yoursite.com/ and MAY be followed by .php (you wouldn’t want to end up with page-one.php.php) and the $1 only grabs that first set of parens anyway.

I just need to redirect all the www.site.com/example-1/article-title.htm to the new location www.site.com/example-2/article-title

Yup, this one IS a redirect (though also a rewrite).

The redirection is from the folder example-1 to the folder example-2.

The rewrite is from example-2/some-page to whatever the REAL page is (is it example-2/some-page.php??).

I used
Quote:
RewriteEngine on
RewriteBase /
RewriteRule ^example-1/(.+).htm$ example-2/$1 [R=301,L,QSA]

but when I access an old article at the old URL www.site.com/example-1/article-title.htm nothing happened … no redirection … nothing …

I didn’t see where you escaped the . in the .htm extension.

In the left-hand side of the RewriteRule, you are trying to make a match. This means that special chars (as DK said) need to be escaped.

On the right-hand side of the RewriteRule, that’s your real resource URL. You don’t escape stuff there.

So you have
RewriteRule ^example-1/(.+).htm$ example-2/$1 [R=301,L,QSA]
It’s close. I dunno that you need the QSA, I thought that was default??
Questions:

  • Do absolutely ALL the urls in example-1 folder end in .htm?
  • What do the new URLS (the real, ugly ones that actually bring up real pages) end with?

If they really end with .php, then you would place this example-1 example-2 line right before the other line above where you just redirect stuff without the .php ending to the real .php page. This means you wouldn’t end the first line with [L] because you do want it to go through the next line too.

You might have already said what all the pages really end in but I’m easily confuzeld and may have missed it… sorry : )

If I screwed anything up too badly, I expect DK to give me a good head-bashing, but I think I’m on the right track.


  1. -\w\d ↩︎

Mallory,

Do YOU think you need a good head-bashing? I don’t. I’ve been using my old IP “motor mouth” to get the message across but NOT being successful. IMHO, I must thank you for your help!

:tup:

Regards,

DK


  1. -\w\d ↩︎

I’m one of the too many :smiley:

Okay so, I’ll just try to understand what Stomme poes said, test the codes again and come back

Thanks

Quote:
the URLs were redirected (no internal server error this time!) .. it means if I visit www.site.com/page-one.php it redirects me to www.site.com/page-one (which is good)
No, it's not!
Quote:
but I got "page not found" ........
because the resource "yoursite.com/page-one" does not actually exist.

You DON'T want www.yoursite.com/page-one.php to get redirected! What you want, a rewrite, is that IF someone types in 
www.yoursite.com/page-one.php 
then that's what they get like before (sorry, however if you change all the hyperlinks in your pages and ads etc then they won't be going here very often)
BUT
if someone types in (or clicks a link)
www.yoursite.com/page-one
that the user does not see a new url (they see the page-one without the .php like you want), but DOES see the content at
www.yoursite.com/page-one.php
(because this is the real page who actually exists in your system)

So what a rewrite (not redirect) does is make other, prettier URLs actually bring back the real, ugly URL's resource (page).

Quote:
it's rewrite not a redirection as it seems ..
Yup, you got it.

you understood me 100% …

I didn’t even know what I wanted from the beginning …

so what I want, is just redirecting all the page-one to page-one.php …

but this will cause some problems because some pages are actually like page-one …

so never mind … .php pages it is ! !!!
Ugly but working URLs … it’s better this way !!!

but for the other issue, it’s a must !!

and there is a difference, because the new URLs exist already , so I just need to permanently redirect all the old URLs to the new ones !

I need to redirect all the old urls

www.site.com/example-1/article-title.htm

to the new ones

that’s it ! there is no .php , that was in the other problem !!

Questions:

- Do absolutely ALL the urls in example-1 folder end in .htm?

yes, but if there are other extensions, I don’t care … I will remove that folder , I just need the articles to be redirected … that’s all

  • What do the new URLS (the real, ugly ones that actually bring up real pages) end with?

the new URLs aren’t ugly! the new URLs are www.site.com/example-2/article-title

www.site.com/example-2/article-title

What I don’t understand is how this can be a page (without some rewrite somewhere)… I could be wrong but I thought all files (files who are not directories) had a file extension.

With the first part you had about removing the .php ending, that was simply a way to tell apache that IF someone asked for a URL that ended with something-or-other without an extension and wasn’t a directory, to look for a file ending in .php.

So you can currently type in your

and your browser will display a web page?

If it does, consistently, then for redirect… I wonder if you can just stick to mod_alias and use RedirectMatch

RedirectMatch 301 example-1/(.+)? http://www.yourserver.com/example-2/$1

Where I’m assuming example-1 is maybe a couple of things… otherwise maybe just plain old Redirect?
Redirect 301 /example-1/ http://www.yoursever.com/example-2/

I’ve never used those two so I have no experience in them. Redirect is the cheapest one to use for redirects where there are no regular expressions needed… if you need a regular expression there’s RedirectMatch. Also cheaper than mod_rewrite, if it works for you.

Hi …

yes if I type the new URL, there is actually a page … so as you said a plain redirection will do it …

If I use

RedirectMatch 301 example-1/(.+)? http://www.yourserver.com/example-2/$1

when I access

it redirects me to

www.site.com/example2/article-title.htm?qstr=article-title

instead of

www.site.com/example2/article-title


and if I use

Redirect 301 /example-1/ http://www.yoursever.com/example-2/

it gives me the same result as the above …

so there is a problem with the .htm or something ?

NO!

The problem is the difference between a URI that Apache can serve and a URI that’s pretty but means NOTHING to the server. Unless you’re following the “remove .htm” code with something else (to add the .php or redirect to a CMS request handler), you SHOULD get 404 (file not found) responses.

It’s time for an update: Please show us your CURRENT .htaccess.

Regards,

DK

I didn’t change anything in my htaccess file , it’s the same …

# -FrontPage-

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

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName mysite.com
AuthUserFile /home/mysite/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/mysite/public_html/_vti_pvt/service.grp




Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

Believe me I’m lost ! it’s the second time in ly life I manipulate this code so I didn’t find a method …

If it’s difficult or can’t happen then I’ll just thank you for your time … I already took too much from your time …

I’ll just leave it like that … the new articles will get indexed anyway … as for the backlinks … I don’t know !!

Regards.

e,

Okay,

[COLOR="Gray"]# -FrontPage-

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

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName mysite.com
AuthUserFile /home/mysite/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/mysite/public_html/_vti_pvt/service.grp

Options +FollowSymLinks[/COLOR]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

That will NOT redirect example1/article-title.htm to example2/article-title.htm NOR to example2/article-title.htm?qstr=article-title (it SHOULD redirect to example2/article-title.htm as should Redirect 301 /example-1/ http://www.yoursever.com/example-2/).

Don’t worry about my time - I’m here to help you learn what you’re doing. HOWEVER, something else is going on which is NOT evident in the .htaccess file. Is there a 404 handler which is parsing the .htm files and serving pages?

Looking back quickly, there was supposed to be a redirection from an extensionless file to a .php file of the same name. Is that implemented elsewhere? You’ve got me confused. Okay, that’s probably because I deal with so many threads every day that continuity is lost in every one without taking an inordinate amount of time to go back to the beginning. You’d realized the php => extensionless was stated backward and then dropped that … but what happened to it (you can’t serve an extensionless file unless you’ve added a TypeHandler for it).

Regards,

DK