issue with url parameters in htaccess - apache

RewriteRule ^/pages/topic\.php?tag=$1 /topics/([^/]+) [R=301,L]
RewriteRule ^topics/([^/]+)/?$ /pages/topic.php?tag=$1 [END]
My problem is with the 301 redirect, I believe the problem is with the tag=$1 and /([^/]+) I'm not entirely sure I've done this correctly, My desired result is that when the user navigates to /pages/topic.php?tag=cryptocom that the user is then redirected to /topics/cryptocom/ Thanks for your help.

With your shown attempts/samples, please try following htaccess rules file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/pages/([^.]*)\.php\?tag=(\S+)\s
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite rules here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^topics/([^/]+)/?$ /pages/topic.php?tag=$1 [NC,QSA,L]

Related

RewriteRule in htaccess only work when I visit url directly

I have htaccess file where I am turning regurlar url example.com/result?from=Brampton&to=Calgary&submit= to Rides-From-Toronto-Calgary-submit seo friendly url. But problem is that I manually have to type url to visit seo friendly version of url, how can I achieve redirect using .htaccess ?
This is what I have so far
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^from [NC]
RewriteRule ^Rides-From-([^/]*)-([^/]*)-([^/]*)$ result?from=$1&to=$2&submit=$3 [QSA,NC,L,R=301]
any help is appreciated.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Also these Rules consider that your internal rewrite is happening to index.php file in case its something else file then please change its name to it in rules.
RewriteEngine ON
##External redirect rules.
RewriteCond %{THE_REQUEST} \s/result\.php\?from=([^&]*)&to=([^&]*)&submit=(\S+)\s [NC]
RewriteRule ^ /Rides-From-%1-%2-%3=%3? [R=301,L]
##Internal rewrite rules from here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:.*?-){2}([^-]*)-([^-]*)-([^-]*)=(.+)/?$ result.php?from=$1&to=$2&$3=$3 [QSA,L]

htaccess error on redirect - too many redirects

I'm trying to redirect all requests from one domain (domain.co.in) to another domain (domain.info.in). I've tried Rewrite directives and Redirect directive in htaccess, but getting too many redirects error in browser. Below is the configuration I'm trying to implement.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.in [NC]
RewriteRule ^(.*)$ index.php/$1
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
My actual working htaccess configuration is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1
Using this, I'm able to redirect all requests to index.php and working fine. But when I add domain rewrite rules, I'm getting the redirect error. I've tried Redirect directive also,
Redirect 302 / http://domain.info.in/index.php/$1, but same error.
I tried the Fiddler tool mentioned in this post, Tips for debugging .htaccess rewrite rules. There it is working good.
Actually, I want to redirect all requests (www.domain.co.in, domain.co.in, www.domain.info.in) to domain.info.in
Any suggestions on this?
As per #CBroe's suggestion, I've updated the configuration and it works. As he said,
RewriteConds always affect the directly following rule.
And I've also added negation to the checking to redirect all other requests.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.co.in$ [NC]
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1 [L]

URL Rewriting Using .htaccess for PHP

I want to convert this URL example.com/post.php?id=12345&title=xyz to example.com/12345/xyz.
I am able to do example.com/12345 but I can't get /xyz.
RewriteEngine On
RewriteRule ^post/([a-zA-Z0-9-/]+)$ post.php?id=$1
RewriteRule ^post/([a-zA-Z-0-9-]+)/ post.php?id=$1
With your shown samples, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.
This solution assumes that you are hitting example.com/post.php?id=12345&title=xyz sample url in browser and want to change it to example.com/12345/xyz
##Enabling engine here.
RewriteEngine ON
##Extrenal redirect 301 to mentioned url by OP in question.
RewriteCond %{THE_REQUEST} \s/post\.php\?id=([^&]*)&title=(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
##Internal rewrite to post.php with query strings.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ post.php?id=$1&title=$2 [QSA,L]

mod_rewrite redirect from old server to new

This sounds like a pretty straightforward task, I would like to redirect a url like:
https://oldserver.com/signup/name to
https://newserver.com/sign_up/?alias=name
Following the mod_rewrite documentation (here) for this scenario fails.
My .htaccess so far is:
RewriteEngine On
RewriteRule ^/signup/(.+) https://newsite.com/sign_up/?alias=$1 [R,L]
# Catch-all
RewriteRule ^(.*)$ https://newsite.com/$1 [R=301,L]
Can anybody tell me what might be wrong?
EDIT
Adding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
before the first rewrite rule still results in a 404
RewriteRule ^/signup/(.+) https://newsite.com/sign_up/?alias=$1 [R,L]
should be
RewriteRule ^signup/(.+) https://newsite.com/sign_up/?alias=$1 [R,L]
(without the / before signup)

Matching code with mod_rewrite

For my application, I want to match an URL with mod_rewrite such that the URL is passed directly to the file (index.php) through a GET request. This is my code:
RewriteEngine On
RewriteRule ^(.*)$ index.php?q=$1
However, it's not working, and it appears to indeterminately rewrite it as "index.php?q=index.php". Could anyone enlighten me on this issue?
Thanks.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?q=$1 [L]
try this one example