简体   繁体   中英

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM