I'm writing to you because of an obstacle i found with .htaccess and Query String. What I need now to achieve is search inside subpages from FORM by GET method. I tried everything found on the internet on yesterday's evening. What partly works is:
RewriteCond %{QUERY_STRING} ^checkin=(.*)&checkout=(.*)$
RewriteRule ^index\.php$ /%1/%2/%3/? [L,R=301]
This script gives a result from:
/chocolate/?checkin=15-08-2016&checkout=18-08-2016
to:
/15-08-2016/18-08-2016/
Probably it's very easy for you to make this subpage be the first before checkin & checkout. I wasted many hours but couldn't make it work properly. I mean:
/chocolate/15-08-2016/18-08-2016/
I even tried to send this subpage's name through INPUT type HIDDEN but it also wasn't working.
You are not mentioning chocolate anywhere:
RewriteCond %{QUERY_STRING} ^checkin=([^\s&]+)&checkout=([^\s&]+)$
RewriteRule ^index\.php$ /chocolate/%1/%2/? [L,R=301]
Related
I've tried looking my question up, but the closest answers I've found didn't work--especially since I'm VERY new to editing .htaccess files.
I have a site that has been programmed to dynamically generate copies of a page to fit a location. For instance, example.com/help/work/ was set up to make about 100 duplicates that look like this: example.com/help/work/?city=Washington&state=DC with the city and state dynamically changing with each page.There are tons of these variations and I want to 301 redirect all the pages with a city and state parameter so they point to the original page (example.com/help/work/).
After some research, I was able to find a RewriteRule that helped me do this on a page by page basis, but only with the homepage:
RewriteCond %{QUERY_STRING} ^city=Philadelphia&state=PA$
RewriteRule ^$ http://example.com/? [R=301,L]
With all that said, I have a two part question:
Is there a way to write this so that it targets subdirectory pages? (I could only get it to do the index)
Is there a way I can use a wildcard like (.*) in a single RewriteRule so example.com/help/work/?city=Washington&state=DC and all its city/state variations point to the original page (example.com/help/work/)?
I'm a bit confused on your request. It appears you want to point every city and state to this single page. http://example.com/help/work/ See if this is what you're looking for.
RewriteCond %{QUERY_STRING} city=.+&state=.+
RewriteRule ^([^/]+)/([^/]+)/?$ http://example.com/$1/$2/? [R=301,L]
Yes, You can use a regex capture group in Rewrite rule that captures the request_uri dynmically .
Like this
RewriteCond %{QUERY_STRING} ^city=.+&state=.+$
RewriteRule ^(.*)$ http://example.com/$1? [R=301,L]
I have googled for a while now, but can't seem to find a solution, so I'll tailor a question directly to my problem.
I am working on a football/soccer website, and entered all the matches. Each database row has a field with a virtual filename in it, like "2010-season/26-may-2010/hometeam-vs-awayteam.php"
I want --> /matches.php?url=2010-season/26-may-2010/hometeam-vs-awayteam.php
to look like --> /matches/2010-season/26-may-2010/hometeam-vs-awayteam.php
My first guess was that the rewriterule would look like this:
RewriteRule ^/matches/(.*)$ /matches.php?url=$1 [L]
But that only gives me a 404 error. I tried different approaches, but nothing solved my issue.
Is the problem that multiple subfolders are passed on into a single parameter?
This should work for you:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /matches\.php\?url=(.*)\ HTTP
RewriteRule ^ /matches/%2? [R=301,L]
RewriteRule ^matches/(.*)$ /matches.php?url=$1 [L]
It will redirect /matches.php?url=2010-season/26-may-2010/hometeam-vs-awayteam.php to /matches/2010-season/26-may-2010/hometeam-vs-awayteam.php and then silently rewrite it back to /matches.php?url=2010-season/26-may-2010/hometeam-vs-awayteam.php.
In the matches.php file use $_GET['url']; to get the url parameter.
I'm trying to 301 redirect a paginated blog list from an old site onto a new url.
I think I'm getting pretty close with the RewriteRule but I'm not quite there yet, this is what I have:
RewriteCond %{QUERY_STRING} ^page=
RewriteRule ^(blog)?$ http://www.newdomain.com/news/page/$1? [R=301,L]
Using this rule if I go to
http://www.olddomain.com/blog?page=1
I currently get redirected to
http://www.newdomain.com/news/page/blog
I would like to be sent to
http://www.newdomain.com/news/page/1
I'm sure its just something small and simple that I'm missing.
Edit
Expanding on the solution below, I've added tags/category support to the rewrite rule using $1.
RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
RewriteRule ^blog/tag/([^/\.]+)?$ http://www.newdomain.com/news/tag/$1/page/%1? [R=301,L,NC]
Few minor mistakes in your code.
You need to capture page parameter's value from query string first
Then use that capture value using % instead of $1
No need to capture blog since you don't need it.
Change your code with:
RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
RewriteRule ^blog/?$ http://www.newdomain.com/news/page/%1? [R=301,L,NC]
Alright, title is REALLY sloppy.
Here's my problem: I have a news site and when you go to the main page (domain.com) it redirects you to domain.com/news/top?geography=San_Francisco after it figures out your geography.
How do I use the .htaccess so that it goes from domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top ?
There are some similar questions, but I have not found one similar enough in that you're editing the URL as a furtherback subdirectory.
It should also be noted that I am using the Code Igniter framework for PHP and it normally has it as domain.com/index.php/news/top?geography=San_Francisco but I did a mod_rewrite already to get rid of the index.php. The code is as follows for that:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Code I've tried:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Before the index.php rule that you have, try adding this:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
You'll need to make sure the links you generate are in the form of domain.com/San_Francisco/news/top though.
But to take care of the links in the wild that still look like the old way, you have to match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]
This will 301 redirect the browser if someone goes to the link domain.com/news/top?geography=San_Francisco and make it so the browser's address bar says this: domain.com/San_Francisco/news/top. At which point the browser will send another request for the second URL, and you use the rule above to change it back into the one with a query string.
Well, I am stuck again. Two days of reading and again, found some close solutions but, nothing fits and all my experiments failed.
This is a continuation of my question:
here at stackoverflow
The 4 rules below take my incoming links:
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
and beatifies it.
Also allows users to use the beatified version right in address bar:
http://somedomain.com/1234567
Here are my working rules:
RewriteRule ^([\s]*)$ /getme.pl [L] ## in case there is a space or nothing.
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1&rewrite [L]
RewriteCond %{QUERY_STRING} partnum=([0-9]*)$
RewriteRule (.*) /%1? [L,R=301]
Works great but, I discovered there are some old links to the site out there:
http://somedomain.com/oldversion.php?id=123456789
And
http://somedomain.com/oldversion.php?r=86this&id=123456789
I would like to just grab the id=[0-9] and integrate it with my working rules.
I suppose, the rule would be inserted between the second and third rules above.
I tried various attempts (about 100!) like:
RewriteRule ^(oldversion\.php)?([a-z]{1})=([a-z0-9]*)&([a-z]{2})=([0-9]*)$ /$4? [L]
RewriteRule ^(oldversion\.php)?([a-z]{2})=([0-9]*)$ /$3? [L]
As you see, two days of reading and nothing is sinking in for me.
I tried several variations of the working rules I already have as well, to no avail.
Can't I just get the 123456789 off of the outdated .php urls somehow and stick it in my existing rules?
Thanks for your help and explaining down to my level co, I just might be able to understand...
Put this at the end of your .htaccess file:
RewriteCond %{QUERY_STRING} id=([^&]+)(&|$) [NC]
RewriteRule ^oldversion\.php$ /%1? [L,R=301,NC,NE]
For a URI of /oldversion.php?r=86this&id=123456789 it will internally redirect to /123456789
Remember RewriteRule just matches your URI and it cannot match your QUERY_STRING.