Link to home
Start Free TrialLog in
Avatar of kbit
kbit

asked on

URL rewrite with htaccess

ASKER CERTIFIED SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kbit
kbit

ASKER

Hi, thanks for the suggestion.

I've just created a htaccess file, containing only those 3 lines, changing the website address to mine.

I've uploaded it into the root of my website (stored on a shared hosting package) but when I put in "http://www.mywebsite.com/red-iron-kettle-100000001" as the URL, I get a an error "Error occurred: 404 - not found"
First, make sure your .htaccess files are being recognized.  An easy way to do this is introduce a syntax error into the file (e.g., a misspelling of 'RewriteRule').  If you get a 500 error, then your file is being processed.  If not, you'll need to use the AllowOverride directive in your server/vhost config.  Most directives in mod_rewrite require the FileInfo override.

If your rules are being processed, but you're still seeing the same behavior, you will need to turn on your rewrite log.  How you do this depends on your Apache version (2.2, 2.4).  Once you have your log working, post the log entries for a single failed request, and we can troubleshoot.
Avatar of kbit

ASKER

If I put in a deliberate error, the webpage gives me an error.

As I'm using a shared server, I don't have access to the config of the server and cant stop/start Apache. Is there any other way I can enable the log?

Many thanks
There could be, depending on what your host offers you as far as control.  If you'd like to continue down that road, contact their support team to ask about lighting up the rewrite log.

Since we know your .htaccess is working, we can try a couple other approaches...

1) Change the flags on the rule from [QSA] to [QSA,R].  If the rule is firing, you should see an actual redirect in your browser.

2) Try putting in a generic catch-all rule to test for ANY redirection (hint: make sure /my-test-page.html exists):
RewriteCond %{REQUEST_URI} !^/?my-test-page.html$ [NC]
RewriteRule ^/?(.*)$ /my-test-page.html?orig=$1 [NC,L]

Open in new window

Avatar of kbit

ASKER

Just reading this comment again:

"Note that this depends on the URL ending with a dash followed by 1 or more numbers only, as shown in your examples.  You can edit the (.*-[0-9]+) portion of the rule to change what it picks up."

In my case, would it not be something like this, with everything after the = being used:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule /?(.*=[a-z0-9]+)$ http://www.mywebsite.com/viewlisting.php?listing=$1 [QSA]

Open in new window

Your sample URLs did not have an equal sign in them.  If you're referring to the equal sign in the query string parameter (e.g., listing=), that is not part of the URL - that's part of the query string.
Avatar of kbit

ASKER

Ah ok, thanks. I'll see if I can get this running locally, this would allow me to change the Apache settings