antivinegar
antivinegar

Reputation: 447

url rewrite and redirect with folder using htaccess

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?

Upvotes: 3

Views: 174

Answers (2)

anubhava
anubhava

Reputation: 785146

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'];`

Upvotes: 3

arco444
arco444

Reputation: 22831

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.

Upvotes: 0

Related Questions