4 Replies - 1132 Views - Last Post: 10 January 2015 - 01:41 PM

#1 longbeachbum   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-December 14

Need help with url rewrite code in .htaccess file

Posted 24 December 2014 - 03:36 PM

Hello, I could use some help from anyone who is very familiar with URL rewriting within the .htaccess file.

I have a single master html file from which I am trying to utilize a URL rewriting code in the .htaccess file to produce numerous additional pages, each of which is targeted to a particular neighborhood.

I think I have it mostly figured out but I'm still running into an issue that I can't seem to resolve:

I have the master html file labeled as: Neighborhood.html and the file name/url is to be rewritten as, for example,: Los-Angeles

And here's the code I put into the htaccess file to accomplish this:

RewriteEngine On
RewriteRule ^Los-Angeles/?$ Neighborhood.html [NC,L]



When I search for the new page in my browser (http://www.example.com/Los-Angeles), I keep getting a 404 error (see below):

"The requested URL /commercial_losangeles/commercial_losangeles/Neighborhood.html was not found on this server."


So, for some reason the code I inserted into my htaccess file is causing the server to request a non-existent sub-folder (/commercial_losangeles/) from within my root folder (which is also /commercial_losangeles/).

It should simply be requesting: /commercial_losangeles/Neighborhood.html

Any idea what I'm doing wrong here?


By the way, I experimented with creating a sub folder containing the 'Neighborhood.html' master page and labeling the sub folder 'commercial_losangeles'...that seemed to fix the problem - the browser rendered the master html page and it also displayed the correct new url: '/Los-Angeles'....but it seems to me that this is just a makeshift way of solving a problem that shouldn't be occurring in the first place.

Your assistance would be greatly appreciated. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with url rewrite code in .htaccess file

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6703
  • View blog
  • Posts: 31,084
  • Joined: 10-May 07

Re: Need help with url rewrite code in .htaccess file

Posted 24 December 2014 - 06:29 PM

I hope that I understand what you are trying to do :

Start URL : http://www.example.com/New-England
Finish URL : http://www.example.c...ighborhood.html

Rather than make an entry for every city, just wild-card it.

RewriteEngine On
RewriteRule (.*) $1/Neighborhood.html [NC,L]



Tested with http://htaccess.madewithlove.be/
Was This Post Helpful? 1
  • +
  • -

#3 longbeachbum   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-December 14

Re: Need help with url rewrite code in .htaccess file

Posted 25 December 2014 - 09:39 AM

View Postno2pencil, on 24 December 2014 - 06:29 PM, said:

I hope that I understand what you are trying to do :

Start URL : http://www.example.com/New-England
Finish URL : http://www.example.c...ighborhood.html

Rather than make an entry for every city, just wild-card it.

RewriteEngine On
RewriteRule (.*) $1/Neighborhood.html [NC,L]



Tested with http://htaccess.madewithlove.be/


Thanks, that's a very interesting idea. I tried using the code you suggested but it isn't working - when I try to open a given page in my browser, I'm getting this error message:

500 Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.


Any idea what is the problem?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6703
  • View blog
  • Posts: 31,084
  • Joined: 10-May 07

Re: Need help with url rewrite code in .htaccess file

Posted 25 December 2014 - 01:36 PM

500 is a server side error.

2 questions :

1.) What happened to the url? What did you give & what did it end up?
2.) What does your Apache logs say? They will provide much more information than me guessing what happened.
Was This Post Helpful? 0
  • +
  • -

#5 guyfromri   User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 836
  • Joined: 16-September 09

Re: Need help with url rewrite code in .htaccess file

Posted 10 January 2015 - 01:41 PM

Two things I learned about this.

1) You have to write the rules for each rewrite line.
2) The rewrite base is actually the source. So if you're on a localhost right now, the base would be localhost/yoursite/neighborhood. If your file that you want to access, neighborhood in the case, is in a subdirectory, the base has to show the whole path.

Also, the dollar sign represents a variable. In this case it's variable "1". If you want neighborhood.html to have that variable in the get, you'll have to pass it as I show below. The conditions show it not to look at file names and directories the same as it would a file that could cause a 404.

Try this. There are a few variations but this should help.

RewriteEngine On
RewriteBase /yoursite/path_to_neighborhood.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Los-Angeles/?$ Neighborhood.html/$1 [NC,L]


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1