I've been struggling with some htaccess redirects. I just spent some time reading and searching on stack and couldn't get an anwser that works with my scenario.
I'm in the process of making the 301 redirect for an old client website to a new one. The old pages has parameters query which I want to remove from the url.
/menu.php?idCategorie=29&idDetail=172
to
/new-website-page/
I have multiple queries to do, here's a couple example:
/menu.php?idCategorie=29&idDetail=172
/menu.php?idCategorie=29&idDetail=182
/menu.php?idCategorie=29&idDetail=184
/menu.php?idCategorie=29&idDetail=256
Which all link to different new pages.
Here's what I tried:
RewriteCond %{QUERY_STRING} idDetail=172
RewriteRule ^menu.php(.*) /new-page/? [R=301,L]
I get redirected correctly, but the URL keeps the query string:
http://website.com/new-page/?idCategorie=29&idDetail=172
I also tried this:
RewriteRule ^menu.php?idCategorie=29&idDetail=172$ http://website.com/new-page/? [L,R=301]
And this:
RewriteCond %{QUERY_STRING} idDetail=172(.*)$
RewriteRule ^menu.php /new-page-name?$1 [L,R=301]
And it didn't work (Still have the query string at the end)
Thanks!
You can use this rule:
RewriteRule ^menu\.php$ /new-page-name? [L,R=301]
Take note of trailing ? in the end which is used for stripping off any existing query string in the original URI.
In addition to anubhava's answer you can alternatively use the QSD flag from Apache 2.4.0
RewriteRule ^menu\.php$ /new-page-name [L,R=301,QSD]
http://httpd.apache.org/docs/current/en/rewrite/flags.html#flag_qsd
Related
I'm trying to redirect a query string to a non query url. It looks something like this.
Original
http://searchtest.help.org/search?client=Cool_Sub&site=Cool_Sub&proxystylesheet=Cool_Sub&q=stackoverflow
Expected result
https://searchtest.help.org/cool-search/stackoverflow
I only want to grab the last part of the query. In this case stackoverflow.
This part will always be static.
search?client=Cool_Sub&site=Cool_Sub&proxystylesheet=Cool_Sub&q=
I'm using this in my apache vhost
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/search$
RewriteCond %{QUERY_STRING} ^client=Cool_Sub&site=Cool_Sub&proxystylesheet=Cool_Sub&q=(.*)
RewriteRule ^(.*)$ https://searchtest.help.org/cool-search/%1 [R=301,L]
However the results with that is
https://searchtest.help.org/cool-search/stackoverflow?client=Cool_Sub&site=Cool_Sub&proxystylesheet=Cool_Sub&q=stackoverflow
I've tried many variations of the above rule but no dice. Any assistance would be grateful.
Add QSD flag to your RewriteRule
RewriteRule ^(.*)$ https://searchtest.help.org/cool-search/%1 [R=301,QSD,L]
According to Apache 2.4 documentation:
When the requested URI contains a query string, and the target URI
does not, the default behavior of RewriteRule is to copy that query
string to the target URI. Using the [QSD] flag causes the query string
to be discarded.
Here is are my rewrite rules and conditions
#link ReWrite
RewriteRule ^searchresults/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /searchresults.php?search=1&year_search_type=single&isused=$1&year=$2&make=$3&model=$4&price=$5 [L]
#query Rewrite
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{REQUEST_URI} ^/searchresults.php [NC]
RewriteCond %{QUERY_STRING} ^.*[&]?isused=(\w+).*&year=(\w+).*&make=(\w+).*&model=(\w+).*&price=(\w+).*$ [NC]
RewriteRule . /searchresults/%1/%2/%3/%4/%5? [R=301,L]
Situation -
i have a search page that searches the database and shows results. This setup catches the Query and rewrites it to clean urls (awesome!) BUT if i run a query a second time, aka from the cleaned url, the search request now just adds the original ?key=value&key=value to the clean url to make the request.
searchresults.php?make=value&model=value turns into searchresults/value/value as i want it to
but when i run the second query from searchresults/value/value it ends up turning it into
/searchresults/value/value?make=value&model=value
any way to defeat that from within .htaccess as the search query itself is in some deep php crazy land of code, and i honestly cant even find the thing, so editing the php is not in the cards.
ALSO, as a bonus, if anyone can tell me what edits to make so that when the query sends a request that looks like key=value+with+spaces i get the correct clean url back? right now it takes key=value+with+spaces and gives me back "value" but drops off with+spaces and moves onto the next key value pair....
THANKS
Not sure why you're having the problem, your rules worked perfectly fine for me on a vanilla apache install in a blank htaccess file. But you can try changing your rules a little:
The first thing you need to do is swap the order of your two rules. The redirect rule must be before any internal rewrites. This prevents two rules from being applied to the same request (the rewrite engine loops through all the rules until the URI stops changing). Second, you need to match against the actual request instead of the %{QUERY_STRING} match against the %{THE_REQUEST} variable:
RewriteCond %{THE_REQUEST} \ /+searchresults\.php\?.*[&]?isused=(\w+).*&year=(\w+).*&make=(\w+).*&model=(\w+).*&price=(\w+).*$ [NC]
RewriteRule ^ /searchresults/%1/%2/%3/%4/%5? [R=301,L]
RewriteRule ^searchresults/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /searchresults.php?search=1&year_search_type=single&isused=$1&year=$2&make=$3&model=$4&price=$5 [L]
So simply, I am trying to redirect specific pages to my 404 page.
I am doing this via a regex. Super new to .htaccess and mod_rewrite.
Basically, I am trying to send all pages in a specific format to my 404. For example:
http://mydomain.com.au/hello.html?wvsessionid=847180243
http://www.mydomain.com.au/gadfg.html?wvsessionid=dsvf8ya87adfg.744
All the above pages should go to my 404 page.
Here is what I came up with, although it doesn't work at all. Doesn't return an internal error or anything, it just lets the pages still be viewed. Not to sure why since I'm still new to regex.
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com\.au
RewriteRule ^/?(.*)/(wvsessionid)$ /$1.html?wvsessionid=$2 [L,R=404]
Does anyone have any idea on how I can fix this to make it work?
You can't match against the query string (everything after the ?) in a rewrite rule. You need to match against the %{QUERY_STRING} var in a rewrite condition:
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com\.au [NC]
RewriteCond %{QUERY_STRING} (^|&)wvsessionid=
RewriteRule !^/404.html - [L,R=404]
It's my first request here and hopefully I won't upset anyone.
Here's my story:
I have looked all over this place for a solution and wasn't able to find one. Here's hoping that someone can give some input.
I've basically managed to use Apache's mod_rewrite to create SEO-Friendly urls for my website.
E.g. Old path www.hostname/index1.php?session=user is now rewritten as www.hostname/user, which results into a SEO Friendly URL address.
However, the old path is still valid. I need to somehow redirect all the incoming old index1.php requests to the newer URLs, the SEO-Friendly ones, for the search engines to transfer the link popularities to the new ones. I believe I may have an infinite-loop redirect and that's why it's not working.
My code so far:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user [QSA,L]
The above rules never get hit when the htaccess file is parsed.
It hit me that I might be doing some sort of redirect loop here so I thought about renaming the index1.php file to index2.php and create something like:
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index2.php?session=user [QSA,L]
However, that failed too.
What would be the best approach to this? What am I doing wrong here?
Thank you!
Update your .htaccess rules to
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteCond %{QUERY_STRING} !no-redir [NC]
RewriteCond %{QUERY_STRING} session=user [NC]
RewriteRule ^/?index1\.php$ /user? [R=301,NC,L]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user&no-redir [QSA,NC,L]
You can't match against the query string (everything after the ?) in a rewrite rule, so you can't match against the session= part. You also can't simply match against the %{QUERY_STRING} var because that gets populated by you other rule when it rewrites the SEO friendly URL to the one with the query string. So you need to match against the actual request:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index1\.php\?session=([^&]+)&?([^\ ]*)
RewriteRule ^ /%2?%3 [L,R=301]
Search engines crawl the site with parameters
/?p=([0-9]+)
/?cat=([0-9]+)
/?do=([a-z]+)
etc
How make in .htaccess to inform the search engines that such pages no longer exists?
I have only /?page=([0-9]+)
Thanks.
I tried
RewriteCond %{QUERY_STRING} ^(p|cat|do)(.*)$
RewriteRule ^$ http://test.com/simple [R=301,L]
but
http://test.com/?p=23 give me http://test.com/test?p=23 (not http://test.com/test)
http://test.com/?cat=11 give me http://test.com/test?cat=11 (not http://test.com/test)
I would suggest following 301 (Permanent Redirect) rule for you:
RewriteCond %{QUERY_STRING} !^page=.*$ [NC]
RewriteRule ^$ http://test.com/test? [R=301,L]
This will redirect every /?p=([0-9]+) or /?do=([0-9]+) or /?foo=([0-9]+) or /?bar=([0-9]+) etc (any query string except /?page=) to http://test.com/test with R=301 and remove the original query string (notice ? in the end of target URL).
You could add those pages to your robots.txt file to prevent the indexing.
However, somewhere the search engines are finding these links. You should ensure that you are not using them on your site.
If the pages no longer exist, you might consider redirecting (301) them accordingly.
The latter, or some combination therein should sort you out.