URL rewrite with htaccess

kbitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve BinkCommented:
Try this:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule /?(.*-[0-9]+)$ http://www.mywebsite.com/viewlisting.php?listing=$1 [QSA]

Open in new window


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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kbitAuthor Commented:
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"
0
Steve BinkCommented:
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.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

kbitAuthor Commented:
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
0
Steve BinkCommented:
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

0
kbitAuthor Commented:
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

0
Steve BinkCommented:
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.
0
kbitAuthor Commented:
Ah ok, thanks. I'll see if I can get this running locally, this would allow me to change the Apache settings
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.