Redirect URL with htaccess - mod-rewrite

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.

Related

How to redirect httaccess

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]

Apache rewrite : how avoid to display url parameter when the url final slash is omitted?

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'] );

URL 301 rewrite with mod_rewrite if contains variable ?tag=xxx

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.

mod_rewrite "400 Bad Request" problem

I can't seem to get past a Bad Request error while setting up mod_rewrite. I've been trying for a while, so here's what I have.
The url I'm trying to access is:
gordons.local/brewCalc
The page I'd like to see is
gordons.local/index.php?page=brewCalc
Here's my rewrite rule:
RewriteEngine on
RewriteLog /var/www/gordons.com/logs/rewrite.log
RewriteRule ([^/]+)/?$ index.php?page=$1 [L]
I've used a regex tool, and this tool, but no matter what I end up with a page that says:
Bad Request
Your browser sent a request that this server could not understand.
Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.2.12 (Ubuntu) Server at gordons.local Port 80
Also, I'm not getting any information in my access, error or rewrite logs.
EDIT: My rewrite rules are in my vhost file. (/etc/apache2/sites-available/gordons.local)
In case anybody ever finds themselves here, my issue was a missing leading slash before the replacement.
RewriteRule ([^/]+)/?$ index.php?page=$1 [L]
Should have been
RewriteRule ([^/]+)/?$ /index.php?page=$1 [L]
Grrrr....
If you see Apache's error.log you would be able to see the actual error. Most likely you are trying to put above rules in .htaccess file and RewriteLog is not allowed in .htaccess file. Also your RewriteRule will redirect more than you intend. So if you comment out your RewriteLog and have your RewriteRule like this then it should work:
RewriteEngine On
RewriteBase /
# request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
# request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
# forward to index.php
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA,NC,NE]
NC - Ignore case comparison
NE - Do not encode RHS URI
QSA - Append existing Query String into new one
L - Mark it last rule

rewriting single url help

I'm trying to rewrite the following url:
index.php?route=checkout/cart
to
/cart
using:
RewriteRule ^index.php?route=checkout/cart$ /basket [L]
However it doesn't seem to work. Anyone know what I'm doing wrong?
Thanks
RewriteRule does only test the URL path. You need RewriteCond to test the query:
RewriteCond %{QUERY_STRING} ^route=checkout/cart$
RewriteRule ^index\.php$ /basket [L,R=301]
The additional R=301 flag will cause an external redirect with the status code 301 (permanent redirect) instead of an internal redirect.
And if you want the other way round:
RewriteRule ^basket$ index.php?route=checkout/cart [L]
You need to send a redirect so that the new URL get reflected in browser address bar. So, add R to the [L].
RewriteRule ^index.php?route=checkout/cart$ /basket [R,L]
If you'd like that searchbots should ignore the "ugly" URL and/or remove it from the indexes and use the new instead, then send a 301 redirect.
RewriteRule ^index.php?route=checkout/cart$ /basket [R=301,L]

Resources