3

OK I'm new with the issue of URL rewriting and redirecting. I've search up and down on Google & Stackoverflow for an answer that would work and nothing seems to work!!

What i'm trying to accomplish is... Take this link: mysite.com/research/index.php?quote=goog

Then convert it and redirect it to this: mysite.com/research/goog

Does it matter that the "goog" at the end of the URL string is grabbed from a form placed in the url? here is the code used to grab the "goog" <?php echo $_POST['quote'];?>

Below is the only snippet code that I have on my htaccess file and it won't work! Am I doing it wrong? Is there something missing from my code? I have my code place on the root directory (mysite.com) should i have it in the "research" folder? (mysite.com/research/)

RewriteEngine On
RewriteRule ^quote/([^/]*)$ /research/index.php?quote=$1 [L]

Is it possible my host / server doesn't accept .htaccess files? should I do it in a web.config file? If so how would I convert the above code to a working web.config file?

2 Answers 2

3

These are the rules you will need in your /research/.htaccess file:

RewriteEngine On
RewriteBase /research/

RewriteCond %{THE_REQUEST} /index\.php\?quote=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?quote=$1 [L,QSA,NC]

Access it in your index.php using:

$quote = $_GET['quote'];`
Sign up to request clarification or add additional context in comments.

9 Comments

thanks for your reply... I tried the code provided and it still didn't work here is the form code used on the index page <form method="get" action="research/index.php?=<?php echo $_POST['quote'];?>"> I tried both post & get... when a symbol is inserted in the input section of the form they are sent to the next page (mysite.com/research/index.php?quote=goog) and it carrys over the symbol enter
What is location of your .htaccess? You must use $_GET['quote']; no POST here.
mysite.com/research/.htaccess is the location of file. even if i changed to "GET" it still didn't work. Should the form look like this? <form method="get" action="research/index.php?=<?php echo $_GET['quote'];?>"> this form action is on the index page (mysite.com/index.php)
First let's make it work without using any <form>. With above rules in place what happens when you enter this URL in a browser: http://domain.com/research/index.php?quote=goog?
when I view the link as you presented it just reloads the page and the URL doesn't change I even tried to visit this: mysite.com/research/goog and a 404 error shows up
|
0

This should work for you:

RewriteEngine On
RewriteCond %{QUERY_STRING} "quote=([^&]+)"
RewriteRule ^/research/index.php /research/%1? [R,L]

The query string is a separate part of the request to the URL so you need to check that explicitly. By using the RewriteCond, you can say if the 'quote' parameter exists, you'll capture it's value (by getting all characters that follow it that are not '&'). This populates the %1 variable which you can use in the rewrite itself.

2 Comments

thanks for the reply! for some reason it didn't work. I tried it in both locations root folder & research folder... does it take a while to process? I'm assuming if I push it live & test it then it should work as soon as I push it live right?
This would need to go in the root directory. Not sure how your hosting is configured so can't comment on how to make it apply I'm afraid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.