Url won't redirect using htaccess - .htaccess

i have a issue on the website im working on, i have 2 urls, url1
https://www.fakeurl/fr/histoires-d-art/interview-peintre-anne-baudequin
and url2
https://www.fakeurl/fr/module/_edito/EditoContent?slug=interview-peintre-anne-baudequin
Both url display the same content but i want to redirect url2 to url1 using htaccess, the code i wrote in in htaccess is not working and i dont understand why. This is it
redirect 301 /fr/module/_edito/EditoContent?slug=interview-peintre-anne-baudequin https://www.fakeurl/fr/histoires-d-art/interview-peintre-anne-baudequin

I finally figured it out by myself, i had to change my code to this for it to work.
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /fr/module/_edito/EditoContent\?slug=interview-peintre-anne-baudequin\ HTTP
RewriteRule ^ /fr/histoires-d-art/interview-peintre-anne-baudequin? [L,R=301]

The mod_alias Redirect directive matches against the URL-path only, not the question string, so the above does not match the requested URL.
You need to use mod_rewrite with an additional condition that matches against the QUERY_STRING server variable (or THE_REQUEST). However, you also need to be careful of redirect loops since I assume you are also using a front-controller (that internally rewrites URL1).
Try the following instead, at the top of your .htaccess file:
# Redirect URL2 to URL1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/fr/module/_edito/EditoContent\?slug=interview-peintre-anne-baudequin\sHTTP [NC]
RewriteRule ^ /fr/histoires-d-art/interview-peintre-anne-baudequin [QSD,R=301,L]
THE_REQUEST contains the first line of the request headers and does not change when the request is internally rewritten.
The QSD flag is necessary to remove the original query string from the redirect response.
Test first with a 302 (temporary) redirect to avoid potential caching issues.
UPDATE: Fixed regex to backslash-escape the literal ? and added slash prefix to the RewriteRule substitution string.

Related

redirect all down.php?name=iptools.online extensions to .html

I would like to automatically redirect down.php?name=NAME on the main page of the site.
For example:
http://example.com/down.php?name=iptools.online
to
http://example.com/iptools.online
I tried with this code:
RewriteRule ^([^/]*)\.html$ /down.php?name=$1 [L]
It works, but when using the old URL, it doesn't redirect.
Try the following before your existing rewrite:
# Redirect "/down.php?name=value" to "/value"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^name=([^&]+)
RewriteRule ^down\.php$ /%1.html [QSD,R=301,L]
%1 is a backreference to the preceding CondPattern that captures the value of the name URL parameter.
The QSD flag is required to discard the original query string from the request.
The condition that checks against the REDIRECT_STATUS env var is required to prevent a redirect loop.
Test with a 302 (temporary) redirect, before changing to a 301 (permanent) redirect in order to avoid caching issues.
...on the main page of the site.
Note that you must have already updated all your internal links to point to the desired URL. The redirect above is not to redirect internal links, but to redirect search engine crawlers and 3rd party inbound links to the old URLs in order preserve SEO.

Try to Fix 301 Redirect of a Dynamic URL | Not Working

I'm trying to 301 redirect the URL /topics/blog/dot-net/?page_id=386 to http://www.example.com/dot-net.
I have tried like below:
Redirect 301 /topics/blog/dot-net/?page_id=386 http://www.example.com/dot-net
Above will not work i was sure but just tried.
And this one:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /topics/blog/dot-net/?page_id=386(.*)\ HTTP
RewriteRule ^ /dot-net? [R=301,L]
If you know something about this issue. How i could fix this in .htaccess please give me a suggestion.
A mod_alias Redirect cannot be used to match against the query string. Your mod_rewrite attempt potentially should work. There might be a caching issue (from previous failed attempts). However, if you just want to redirect the stated URL then it can be written something like:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page_id=396$
RewriteRule ^topics/blog/dot-net/$ /dot-net? [R=302,L]
It is more efficient to check the URL-path in the RewriteRule pattern, rather than use a generic catch-all.
You would only need to check against THE_REQUEST if you needed to prevent a rewrite loop - but this is not stated in your question.
Change the 302 (temporary) to 301 (permanent) when you are sure it's working OK.

prefix a friendly url using mod rewrite in htaccess

Is it possible to prefix a htaccess rewrite rule
for example can a variable be used as a prefix to a url
website.com/$variable-for-sale/
/cupcakes-for-sale/
/pies-for-sale/
/flans-for-sale/
The idea is to then use that variable to display all the cupcakes/pies/flans for sale
How would this be written as a rewrite rule? Is it even possible?
Thanks
The first rule will take care of redirecting your ugly URL to Friendly like one.
The second rule will internally redirect it back so the browser URL remains the friendly URL while service the content of your page.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect /?cake=anything to /anything-for-sale/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?cake=([^&\s]+) [NC]
RewriteRule ^ /%1-for-sale/? [R=302,L]
# Internally forward /anything-for-sale/ to /?cake=anything
RewriteRule ^([^-]+)-for-sale/?$ /?cake=$1 [NC,L]
Keep in mind I am using R=302 its always better to use 302 which means temporary redirect while testing a new rule before making it permanent as the permanent will cache the information to your browser. Once the rule is confirmed to be working as expected change R=302 to R=301.
To extract variable, you need to use regex parentheses in the correct pattern, then you can use $1 to fetch the group:
RewriteRule ^([^-]+)-for-sale/$ /target.php?variable=$1 [L]
The "target" part is the script you use to display the "variable". Since your question doesn't mention what that is, you have to figure it out.

Htaccess rule to resolve the 404 error when host name is repeating twice

We are using unlimited sitemap generator to generate a xml sitemap and it takes all urls in the site. Unfortunately there is an error noticed in webmaster's crawl error section. There are a huge number of urls fetching by sitemap as duplicate urls.
For eg:if actual url is "http://www.example.com/forum/viewtopic.php?f=5&t=221&st=0&sk=t&sd=a&start=10"
The sitemap fetch this url and also a duplicate url returning 404 error as"http://www.example.com/http://www.example.com:80/forum/viewtopic.php?f=5&t=221&st=0&sk=t&sd=a&start=10"
(This is only an example url.)
All other urls listed in the sitemap are correct.The issue is with forum section only.(Using phpbb for forum).
Can any one suggest any valid htaccess rule to avoid this 404 .
I want to redirect all patterns like 'http://www.example.com/http://www.example.com:80/forum/....' to 'http://www.example.com/forum/.........'
Any help will be appreciated.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+.+?(/forum/[^\s]+) [NC]
RewriteRule ^ /%1 [R=302,L,NE]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Explanation:
%{THE_REQUEST} represents the original request as received by Apache which in your case may look like: GET /http://www.example.com:80/forum/viewtopic.php?f=5&t=221&‌​st=0&sk=t&sd=a&start=10 HTTP/1.0
Breaking down my regex: ^[A-Z]{3,}\s/+.+?(/forum/[^\s]+) now
This part of regex ^[A-Z]{3,}\s matches 'GET ' part of input.
This part of regex /+.+? matches /http://www.example.com:80 part of input (.+? is reluctant match until next part of regex i.e. /forum/ starts.
This part of regex /forum/ matches literal /forum/ part of input.
This part of regex [^\s]+ matches /viewtopic.php?f=5&t=221&‌​st=0&sk=t&sd=a&start=10 part of input. (until a space is found).
(/forum/[^\s]+) is putting /forum/viewtopic.php?f=5&t=221&‌​st=0&sk=t&sd=a&start=10 in match group #1 (denoted by %1 in RewriteRule later)
Then RewriteRule ^ /%1 [R=302,L,NE] is executing when above RewriteCond is true. This rule then redirects the request to %1 captured above.

trouble with simple mod_rewrite redirect rule

I have mod_rewrite working in a development environment.
This testing domain is using these rules in an .htaccess file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
# deal with potential pre-rewrite spidered / bookmarked urls
RewriteRule ^clothes/index.php?pg=([0-9]+)$ /clothes/index$1.php [R=301,L]
# deal with actual urls
RewriteRule ^clothes/[0-9a-z-]+-pr([0-9]+).php$ /clothes/product.php?pid=$1 [L]
The 2nd Rule works fine. Entering http ://testdomain.dev/clothes/shirt-pr32.php is silently delivered content from http ://testdomain.dev/clothes/product.php?pid=32 ...which is as desired and expected!
However, assuming this was applied to a live site, one that had originally used paths such as: http ://testdomain.dev/clothes/product.php?pid=32, I'd like to redirect any incoming requests following the old pattern to the new urls ...which is what the 1st Rule was intended to do.
My problem is my testing server seems to ignore the 1st Rule and serves the page as requested (page loads but address bar remains at http ://testdomain.dev/clothes/product.php?pid=32)
Any assistance or enlightenment would be most graciously accepted!
You need to match the query string within a RewriteCond, then backreference that RewriteCond from the rule. The RewriteRule only matches against the path, not the query string.
Here's a related post I previously answered with a similar request: Mod_rewrite rewrite example.com/page.php?v1=abc&v2=def to example.com/abc/def
You can't match against the query string in a rewrite rule, you need to use the `%{QUERY_STRING} variable in a condition and use the % to backrefernce groupings. So instead of:
RewriteRule ^clothes/index.php?pg=([0-9]+)$ /clothes/index$1.php [R=301,L]
You'll need:
RewriteCond %{QUERY_STRING} ^pg=([0-9]+)
RewriteRule ^clothes/index.php$ /clothes/index%1.php? [R=301,L]

Resources