I tried to redirect but not working even I put the correct code from take it from web.
Redirect /index.php?page=8 /?page=8
Redirect /index.php?page=8 /?page=8
The mod_alias Redirect directive does not match against the query string, so the above directive will never match, so does nothing.
To remove the index.php (directory index) from the visible URL, you would need to use mod_rewrite at the top of your .htaccess file. For example:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php$ / [R=301,L]
The above will redirect a URL of the form /index.php?page=8 to /?page=8. Any query string present on the initial request is simply passed through to the target/substitution unaltered.
The condition that checks against the REDIRECT_STATUS env var ensures we don't get a redirect loop caused by mod_dir (or the Laravel front-controller) rewriting the request to index.php.
Clear your browser cache and test first with a 302 (temporary) redirect.
However, if you did only want to redirect the specific URL /index.php?page=8 (as stated in the question) to /?page=8 then you should write the rule like the following instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/index\.php?page=8\sHTTP
RewriteRule ^index\.php$ / [R=301,L]
Your htaccess code should be.
RewriteEngine On
RewriteRule ^index.php?page=$1 /?page=$1 [R=301,NC,L]
Related
I have config file in apache that should redirect / request to different url.
But i am getting 404 when i hit /
Request is going to backend server but it is not redirecting.
Config file is
#This is not getting invoked. Returning 404.
#Request is redirected to app2.adobecqms.net with out appending page1/page2.html
RedirectMatch ^/$ https://app1.abc.com/page1/page2.html
RewriteCond %{REQUEST_URI} ^/folder1/folder2
RewriteCond %{REQUEST_URI} !^/(.*)\.(json|html)
RewriteRule ^/(.*)$ http://app2.adobecqms.net/$1.html [P]
RewriteCond %{REQUEST_URI} ^/analytics.txt
RewriteRule ^/(.*)$ http://app2.adobecqms.net/page1/page2/page3/$1 [P]
#I also tried with below. But no luck
#This is returning 502
#RewriteRule ^/$ https://app1.abc.com/page1/page2.html [P]
Any idea?
#This is not getting invoked. Returning 404.
#Request is redirected to app2.adobecqms.net with out appending page1/page2.html
RedirectMatch ^/$ https://app1.abc.com/page1/page2.html
Because you are using a mod_alias RedirectMatch directive, which is processed after the other mod_rewrite (RewriteRule) directives despite the order of directives in the config file.
And/or you have ProxyPass directives that forward other requests? mod_rewrite is required to override this.
You need to use mod_rewrite RewriteRule here also in order to avoid the conflict. For example:
RewriteRule ^/$ https://app1.abc.com/page1/page2.html [R=302,L]
And this directive should be before the existing directives. (Although there shouldn't be a conflict in this case.) The L flag is required.
The P flag sends the request through mod_proxy - this isn't a "redirect".
The RedirectMatch directive you posted would default to a 302 (temporary) redirect, as I used here. However, if this is intended to be permanent then change to a 301, but only once you have confirmed that it works as intended (to avoid caching issues).
I have a domain alias that I want to stay as it is, but I want to redirect the main domain url.
http(and s)://www.alias.maindomain.com -> no redirect
http(and s)://www.maindomain.com -> redirect to given url
I have a Laravel project and this is my htacess.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule "^http://www.maindomain.com" "http://www.redirectedpage.com"
I tried redirecting the routes in laravel but that wasnt working so think the htacess file is the way to go?
To redirect only www.maindomain.com/ to https://www.redirectpage.com/ you would need to do something like the following at the top of your .htaccess file, before the Laravel directives.
RewriteCond %{HTTP_HOST} =www.maindomain.com
RewriteRule ^$ https://www.redirectpage.com/ [R=302,L]
The RewriteRule pattern (the first argument to the RewriteRule directive) matches the requested URL-path only (less the directory-prefix when used in a .htaccess context, so there is no slash prefix). In this case, the RewriteRule pattern ^$ matches an empty URL-path, corresponding to the requested URL / (ie. the document root).
To match the requested hostname you need to use a condition (RewriteCond directive) and check against the HTTP_HOST server variable - which contains the value of the Host HTTP request header.
The CondPattern =www.maindomain.com (with a = prefix) is an exact match string comparison, not a regex.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule "^http://www.maindomain.com" "http://www.redirectedpage.com"
I'm not sure why you are checking that the request does not map to a physical file. If you want to redirect a specific URL, then it presumably does not matter whether that URL maps to a file (or directory).
As mentioned above, the RewriteRule pattern matches against the requested URL-path. So, the regex ^http://www.maindomain.com will never match.
In my .htacess of my domain, I must point subdomain to the 1rst GET parameter of the domain. The subdomain represents the language (for example en., fr, etc...).
In order to achieve this aim, here the rewrite code in the .htaccess :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteRule ^(.*)$ $1?lang=%1 [NC,L]
I create a directory named test. This directory contains just index.html file.
So when you type in the url bar of a browser en.example.com/test/,
the rewrite code works.
But if you type en.example.com/test without the final slash, it redirects to en.example.com/test/?lang=en => it's a problem.
So have you an idea to correct that ?
Thank you in advance, cordially.
When specifying xx.example.com/test/, an internal redirect
occurs to xx.example.com/test/?lang=xx. The client never sees the ?lang=xx.
However, when specifying xx.example.com/test, where test is a directory,
mod_dir steps in and rewrites the URL to xx.example.com/test/, but in
such a way that the rewrite rule for the ?lang=xx redirect becomes public,
having the client see xx.example.com/test/?lang=xx as the URL - which is unwanted.
In order to keep the '?lang=' redirect local (hidden from the client)
place this in the .htaccess, BEFORE the original rewrite rules:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R,L]
The condition checks whether the requested filename is a directory,
and the rule forces a client-side redirect [R], so that a client requesting xx.example.com/test will be redirected to xx.example.com/test/.
The key however is the [L], which makes this rule the Last, preventing the following rules from executing. Without this L flag, the entire redirect from xx.example.com/test to xx.example.com/test/?lang=xx becomes public.
After the client is forcefully redirected to the proper URL with a terminating /, the rewrite rules doing the internal redirect adding the lang GET parameter are executed as normal.
Here's the entire .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R,L]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteRule ^(.*)$ $1?lang=%1 [NC,L]
There is however another way to achieve this without using Apache config and internal redirect, and that is to examine $_SERVER['SERVER_NAME']:
<?php
list( $lang ) = explode('.', $_REQUEST['SERVER_NAME'] );
A third party plugin returns to an incorrect URL from a call to save a change.
The URL is /admin/?page=configure/admin/. The correct return should be to /lists/admin/?page=configure. My attempt to write a redirect failed with a 500 server error.
RewriteEngine On
RewriteRule ^(.*)/admin/(.*)$ $1/lists/admin/$2 [NC,L]
How can I correct this code?
This should work.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^lists
RewriteRule ^(.*/)?admin/(.*)$ $1lists/admin/$2 [QSA,L]
If you want to match a different folder to redirect to admin you will have to declare it literally as a pattern like ^(.*)?/admin would also match lists/admin and cause a loop.
I just can't seem to be able to run a 301 redirect and rewrite when a specific variable is somewhere inside a dynamic URL.
For example, with any of these URLs:
/movabletype/mt-search.cgi?tag=SOMETHING&limit=20
/some-other-random-content?post=somethinghere&tag=SOMETHING
If tag=SOMETHING is anywhere inside the URL, then redirect to:
/categories/something_here/
Any ideas?! Here's what I have so far - I'm at a loss as to what to put inside the RewriteCond
RewriteCond %{REQUEST_URI}
RewriteRule tag=SOMETHING /categories/something_here/ [L,R=301]
Your rewrite condition requires a left- and right-hand argument. It looks like you want to redirect when a certain URL parameter is present (i.e, tag), so you can use %{QUERY_STRING} in your condition.
Consider the following example:
RewriteCond %{QUERY_STRING} tag=([A-Za-z0-9]+)
RewriteRule ^(.*)$ /categories/$1 [R=301,L]
This should take a URL like /some-other-random-content?post=somethinghere&tag=SOMETHING and redirect it to /categories/SOMETHING.
URL Rewriting for Beginners may be a helpful guide.