Changing url path with .htaccess

Hey all,

I am wanting to go from

url.com/porfolio.html

to

url.com/portfolio/

Can anyone direct me where I can find out how its done. I did a g search but couldnt come up with nothing but how to change long urls to shorter ones.

Hi there,

Create a .htaccess file and add the following:

RewriteEngine On
RewriteRule ^porfolio.html$ http://url.com/porfolio/ [R=301,L]

Then upload the .htaccess to the root of your domain and try navigating to url.com/porfolio.html.
Does that work?

404 page

M’kay

The folder url.com/portfolio/ does exist and have an index.html file in it, doesn’t it?
Or are you just wanting to rewrite the url, but still have it point to the original file?

Yea I want it to rewrite the url, I knew I could put up another directory and place an index.html that has the portfolio code but I was trying to make it easier and keep the same structure.

Hi there,

Try this:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^portfolio/$ portfolio.html [NC,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\\ /portfolio\\.html [NC]
RewriteRule ^portfolio.html$ /portfolio/ [L,R=301]

Any joy?

Edit: this should work for the following url scheme: url.com/porfolio.html, i.e.with the file in the top-level-directory

laf,

First, do NOT change a file URI to a folder (using a trailing /). After that, have a look at the tutorial linked in my signature then come back with questions and your attempted code.

Regards,

Dk

Hi there Dk,

I hoped you’d show up :slight_smile:
I hope the OP doesn’t mind me carrying on this thread (if so, just say and I’ll butt out), but after investing a couple of hours reading articles about mod_rewrite, I’m now curious about the best way to do this.

Are you saying that it is a bad idea to change “url.com/portfolio.html” into “url.com/portfolio/
If so, why?

I’ve just finished reading your tutorial (again). The first part was fairly straight forward. The rest made my head explode.

One important thing I took away from what you wrote however, is to be explicit in what you tell Apache to do (and not to do).

So, I guess these are the requirements.

[LIST]
[]If a user requests url.com/portfolio.html, then rewrite the address in the address bar to url.com/portfolio/ (not sure about slash) and then serve the file
[
]If a user requests url.com/portfolio or url.com/porfolio/ redirect internally to url.com/portfolio.html
[/LIST]And these are the steps I would take to solve this problem:

[LIST]
[]Test if the entire query string exactly matches portfolio.html
[
]If so rewrite the query string to portfolio/
[]Check if the query string exactly matches portfolio/
[
]If so, redirect internally to portfolio.html
[/LIST]Can you tell me if that is a sensible approach and maybe the sections of your tutorial I should concentrate on to accomplish this?

Hi Pullo!

P - I hoped you’d show up :slight_smile:

That’s quite a compliment, thank you. From your prior responses, I know that you’re working hard to learn mod_rewrite and have the basics down okay. The rest is all logic … as you get to below.

P - I hope the OP doesn’t mind me carrying on this thread (if so, just say and I’ll butt out), but after investing a couple of hours reading articles about mod_rewrite, I’m now curious about the best way to do this.

It’s fair to ask questions along the same line as the OP.

P - Are you saying that it is a bad idea to change “url.com/portfolio.html” into “url.com/portfolio/
If so, why?

There are several reasons:

  1. Doing so changes the folder level for internal links.

  2. Doing so makes Apache (and visitors) believe that they are linking to a folder’s DirectoryIndex.

  3. Doing so has no benefit.

  4. It just makes no sense (to me) at all!

P - I’ve just finished reading your tutorial (again). The first part was fairly straight forward. The rest made my head explode.

Bummer! :eek2: That must have hurt!

P - One important thing I took away from what you wrote however, is to be explicit in what you tell Apache to do (and not to do).

Amen.

P - So, I guess these are the requirements.

[LIST]
[*]If a user requests url.com/portfolio.html, then rewrite the address in the address bar to url.com/portfolio/ (not sure about slash) and then serve the file

:nono: The OP wants to link to portfolio/ and serve portfolio.html.

[*]If a user requests url.com/portfolio or url.com/porfolio/ redirect internally to url.com/portfolio.html

:nod: That’s what I believe his/her intent was.
[/LIST]P - And these are the steps I would take to solve this problem:

[LIST]
[]Test if the entire query string exactly matches portfolio.html
[
]If so rewrite the query string to portfolio/
[]Check if the query string exactly matches portfolio/
[
]If so, redirect internally to portfolio.html
[/LIST]P - Can you tell me if that is a sensible approach and maybe the sections of your tutorial I should concentrate on to accomplish this?

The “Redirect TO New Format” section has the code to handle a “loopy redirection.”

Pullo, IMHO, you’re going about this all wrong. You’re trying to redirect TO the folder then back to the file (loopy - it can be done safely but that’s more advanced [and IS in the tutorial how to do that safely]).

My list (advanced):

  • Order is critical!
  • Strip file extension (.html) by
    - Check if IS_SUBREQ to prevent looping
    - Redirect to extensionless filename (strip file extension, i.e., .html, NOT .jpg, .css, .js, etc) with R=301
  • Serve file with .html file extension by
    - Check if extensionless filename with .html extension is a file
    - Redirect extensionless filename to include .html file extension and serve (without R=301)

I intentionally used pseudocode to leave the coding as an exercise for you but will be willing to help if you run into a roadblock (even with the “non-loopy code” in the tutorial).

Regards,

DK

Hi DK,

Many thanks for the detailed response!
I’ve taken a while to think this through and in light of your comments, I’ve rewritten the requirements:

[LIST]
[]If a user requests url.com/portfolio.html, then redirect to extensionless file name with R=301. This will give us url.com/portfolio and not url.com/portfolio/, but this is acceptable, as it doesn’t mess with user expectation or change the folder level for internal links.
[
]If a user requests url.com/portfolio or url.com/porfolio/ redirect internally to url.com/portfolio.html
[/LIST]This is the code I have used to do this:

RewriteEngine on
RewriteCond %{IS_SUBREQ} false
RewriteRule portfolio.html$  /portfolio [R=301,L]

RewriteCond %{REQUEST_FILENAME}\\.html -f
RewriteRule ^portfolio/?$ portfolio.html [L]

Unfortunately, this causes an infinite loop.
I’ve done a lot of Googling and found several allusions to this being caused by the initial redirect being treated as a new request (and thus never as a sub-request), but try as I might, I can’t get it to work.

Could you shed some light on this and tell me where I’m going wrong?

Pullo,

You did very well with your code EXCEPT for understanding that the optional trailing / is poison (two different directory levels in play) for relative links in the file.

My “pseudo code” was for a generic extensionless file name but, if you’re only using portfolio, that greatly simplifies the problem (meaning that you don’t need to check if the file exists - you KNOW it does).

[QUOTE=Pullo;5260171]Hi DK,

RewriteEngine on
RewriteCond %{IS_SUBREQ} false
RewriteRule [COLOR="#0000FF"]^[/COLOR]portfolio[COLOR="#0000FF"]\\[/COLOR].html$  /portfolio [R=301,L]

[COLOR="#FF0000"]RewriteCond %{REQUEST_FILENAME}\\.html -f[/COLOR] # not required if you know it exists
RewriteRule ^portfolio[COLOR="#FF0000"]/?[/COLOR]$ portfolio.html [L] # POISON

That should not result in an infinite loop BECAUSE the {IS_SUBREQ} will not be false after executing the redirection to the html file. I don’t know who’s saying that an internal redirection is being treated as a new request but that’s incorrect (mod_rewrite DOES continue to make passes through its code until no matches/redirections are found but the {IS_SUBREQ} is set to true after the first pass (if portfolio.html was originally requested)).

Of course, there may be other mod_rewrite code in action in your .htaccess but you’ve not displayed it so I can’t comment on any conflicting interaction.

Regards,

DK

Hi DK,

Thanks for your reply. I am learning a lot here!

I tried changing the .haccess file to this:

RewriteEngine on
RewriteCond %{IS_SUBREQ} false
RewriteRule ^portfolio\\.html$  /test [R=301,L]

RewriteRule ^portfolio$ portfolio.html [L]

But unfortunately still get an infinite redirect loop and the error message:

(Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

There is no other code in my .htaccess file.
The file “portfolio.html” exists and is in the top level directory of my website along with the .htaccess file.

Am I missing something?
Could it be a server configuration issue?

N.B. If I change IS_SUBREQ to true, then mysite.com/portfolio serves up the file as expected. However, then navigating to mysite.com/portfolio.html also serves up the file without rewriting the url.

Pullo,

Please try again after replacing /test with portfolio (no slash).

Regards,

DK

Hi DK,

Sorry about the delayed response - busy, busy, busy …

Also, sorry for confusing things by leaving “test” in the .htaccess file.
This was just a typo on my part and I have been testing with “portfolio” all along.

Right, now I have this code in my .htaccess file.

RewriteEngine on
RewriteCond %{IS_SUBREQ} false
RewriteRule ^portfolio\\.html$  portfolio [R=301,L]
RewriteRule ^portfolio$ portfolio.html [L]

Unfortunately, still no joy.
When I navigate to either http://url.com/porfolio or http://url.com/porfolio.html
I get redirected to: http://url.com/home/http/power/rid/54/59/53525459/htdocs/portfolio

This was why I had the slash there in the first place.

Hope you can shed some light on it.

Pullo,

Indeed I can - your server is broken (needs restarted) as there is nothing in your .htaccess to redirect wildly like you’ve indicated.

I would also use an ErrorDocument statement to specify that 404s should be redirected to … your 404 script?

Finally, it may be a “logic thing” that, if there hasn’t been a subrequest, the {IS_SUBREQ} may actually be null rather than false. Use !true rather than false and see whether that helps.

As for your trailing /, I remain adamant that trailing /'s on file names is extremely bad technique and should mess-up your server (especially since you’ve probably enabled MultiViews which will defeat the specific mod_rewrite you’re working on).

Regards,

DK

Hi there DK,

Thanks for your reply.
If you think the server is broken, then I think I’ll leave it here.
What we are trying to accomplish would not actually be something I would do in reality and was intended as more of an academic exercise.
Plus the fact, the server belongs to one of the biggest hosting companies in Germany, so I don’t think they’d restart it on my say so :slight_smile:

BTW I tried changing “false” to “!true”, but that didn’t help either.

Thanks again!

Pullo,

Well, if the host isn’t aware of a “minor problem,” they can’t do much about it, can they.

Bummer. Thanks for the feedback on the T/F question.

Regards,

DK