Ezra_Bender
Ezra_Bender

Reputation: 77

Url rewrite not working; 404 error and no url change

I'm working on a website where I want the url

    www.example.com/directory1/states/california.php 

to point to

    www.example.com/directory1/california

And a similar url change for the city pages as well:

   www.example.com/directory1/cities/miami.php

should point to

  www.example.com/directory1/miami

I'm using the following rules in my access file to change the url:

RewriteRule ^directory1/(alabama|alaska|arizona|arkansas|california|colorado|connecticut|delaware|florida|georgia|guam|hawaii|idaho|illinois|indiana|iowa|kansas|kentucky|louisiana|maine|maryland|massachusetts|michigan|minnesota|mississippi|missouri|montana|nationwide|nebraska|nevada|new_hampshire|new_jersey|new_mexico|new_york|north_carolina|north_dakota|ohio|oklahoma|oregon|pennsylvania|rhode_island|south_carolina|south_dakota|tennesee|texas|us_virgin_islands|utah|vermont|virginia|washington|west_virigina|wisconsin|wyoming)$ /directory1/states/$1.php [L]

RewriteRule ^directory1/(.*)$ /directory1/cities/$1.php [L]

However, nothing changes in the url bar and I always get a 404 not found. When I tested it with the htaccess checker, the output url is always correct. What is wrong with my rules? Is there a way to test how/if mod_rewrite is even functioning?

Upvotes: 0

Views: 274

Answers (1)

Ross McLellan
Ross McLellan

Reputation: 1881

Some server configs require you to turn the rewrite engine on in your .htaccess file. Right at the top:

RewriteEngine on

I've also known some server configs where the file path starts/ends with a / (due to other rewrite rules already being run on the request) so perhaps allow for that to be there in the rules:

RewriteRule ^/?directory1/(alabama|alaska|arizona|arkansas|california|colorado|connecticut|delaware|florida|georgia|guam|hawaii|idaho|illinois|indiana|iowa|kansas|kentucky|louisiana|maine|maryland|massachusetts|michigan|minnesota|mississippi|missouri|montana|nationwide|nebraska|nevada|new_hampshire|new_jersey|new_mexico|new_york|north_carolina|north_dakota|ohio|oklahoma|oregon|pennsylvania|rhode_island|south_carolina|south_dakota|tennesee|texas|us_virgin_islands|utah|vermont|virginia|washington|west_virigina|wisconsin|wyoming)/?$ /directory1/states/$1.php [L]

RewriteRule ^/?directory1/(.*)/?$ /directory1/cities/$1.php [L]

Upvotes: 1

Related Questions