Url Rewrite from one page to another - mod-rewrite

I have a URL like
www.testwebsite.com?param1=abc
or
www.testwebsite.com/?param1=abc
I need to redirect to
www.testwebsite.com/level1/xyz.html?customparam=123&param1=abc
I tried working on rewrite url but i am still new to this and need some help to achieve the solution
This is what i did till now
RewriteCond %{QUERY_STRING} param1=(.+)
RewriteRule ^/$ /level1/xyz.html?customparam=123&param1=%1 [R=301,L]

Try adding the following to the .htaccess file in the root directory of your site.
Only param1 will be captured and passed through, other query string params will be dropped.
RewriteEngine on
RewriteBase /
#if the param1 parameter is present
RewriteCond %{QUERY_STRING} (^|&)(param1=([^&]+))(&|$) [NC]
#redirect
RewriteRule ^$ level1/xyz.html?customparam=123&%2 [L,R=301]

Related

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

Trouble redirecting subdomains to a folder

I have got this working:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteRule ^(.*) http://domain.co.uk/%1 [L]
So it will redirect test.domain.co.uk to test.domain.co.uk/test
However, I would like it so that it changes the document root rather than the user seeing the redirect. I have read a few articles on here and tried a few things. But I am not sure how to debug any errors, it just doesn't work. The one other that I have tried:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{HTTP_HOST} ^([^\./]+)
RewriteCond %{DOCUMENT_ROOT}/-%1 -d
RewriteRule ^(.*)$ -%1/$1 [L]
But this doesn't seem to work at all.
Any help would be appreciated.
Ian
Try something like this in your document root:
RewriteEngine On
# check if subdomain folder has the existing file, if so, serve it
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f
RewriteRule ^(.*)$ /%1/$1 [L]
# check if subdomain folder has the existing directory WITH A TRAILING SLASH, if so serve it
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*?)/$ /%1/$1/ [L]
# check if subdomain filder has the existing directory but missing trailing slash, redirect with it
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /$1/ [R=301,L]
This method does 2 things for you, if it's a 404, it won't try to internally rewrite, so if you go to: http://sub.domain.co.uk/this/path/does/not/exist/blah, you won't get a 404 message saying: /sub/this/path/does/not/exist/blah doesn't exist, just /this/path/does/not/exist/blah because the URI wasn't rewritten, and thus not exposing your underlying directory structure.
The second thing is that you probably have DirectorySlash turned on. Which means if you access a directory like: http://sub.domain.co.uk/this/path, missing the trailing slash, mod_dir will want to redirect and add that slash, but it'll probably do it wrong because the URI has been rewritten and will redirect you to: http://sub.domain.co.uk/sub/this/path/. We you pre-emptively add the trailing slash with the correct URI hiding the sub.

mod_rewrite redirect is appending url args I dont want

I have a lot of old urls that I need to redirect. The site works by hitting an index.php?url=whatever via a mod_rewrite rule. However, I dont want the user to see the arg in the case of a redirect:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^clients_whatever1\.html$ / [R]
RewriteRule ^clients_whatever2\.html$ /client-list/whatever2 [R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
So in the case of:
(the domain)/clients_whatever2.html
they are being redirected to:
(the domain)/client-list/whatever2?url=/client-list/whatever2
but it should simply read as:
(the domain)/client-list/whatever2
How to do the redirect so that it shows up in the browser url as any other request (for a non-redirect)?
Append a ? to the rule target to drop the original query string params i.e.
RewriteRule ^clients_whatever1\.html$ /? [R,L]
RewriteRule ^clients_whatever2\.html$ /client-list/whatever2? [R,L]

How to redirect an url with parameters?

I'm at a total loss trying to integrate a mod_rewrite in my existing page. Currently, I am building a new site from scratch, and there i have some nice clean url's such as:
http://www.example.nl/nl/example
The old site, running Cms made simple, has some not-rewritten url's that would need to be redirected to the new pages. Those url's look like this:
http://www.example.nl/index.php?page=cake-and-pie&hl=nl_NL
But shorter versions of that like:
http://www.example.nl/index.php?page=cake-and-pie
also work.
It took me a while to figure out that url's with parameters cannot simply be redirected with "Redirect 301", like i'd normaly do. So i tried some online mod_rewrite generators like this and this, but the rules outputted by those result only in 404 errors, (the redirect doesn't work at all).
My .htaccess file current looks like this:
RewriteEngine On
RewriteBase /
# remove .php;
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
The old pages would seize to exist.
How do i redirect the old pages to the new ones?
Thanks.
EDIT
RewriteCond %{QUERY_STRING} =page=pie
RewriteRule ^index\.php$ /nl/? [L,R=301]
RewriteCond %{QUERY_STRING} =page=pie&hl=nl_NL
RewriteRule ^index\.php$ /nl/? [L,R=301]
Seems to do the trick. This is of course manual for every url, but i only have a few.
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)/([^/]+)/?$ index.php?page=$1&hl=$2
RewriteRule ^([^/]+)/?$ index.php?page=$1 [QSA]

Rewrite URL on submit form

I have a search form and when I make a search I get this URL "http://****/video/view/search/?imeto_tam=tarsene" but I want to replace this the "?imeto_tam=tarsene" with the word I search for and my address to look like this - "http://****/video/view/search/tarsene". Generally I use mod_rewrite on my site and it's working for my links but it's not working for the form-s. Could someone tell me how to do it?
RewriteEngine On
RewriteRule ^view/([0-9a-zA-Z\-\(\)]+)/?$ index.php?a=$1 [L]
RewriteRule ^view/([0-9a-zA-Z\-():]+)/([0-9a-zA-Z\-():\.,]+)$ index.php?a=$1&id=$2 [L]
These rules would go into your root .htaccess file and 301 redirect the querystring imeto_tam to the folder and then the next rule would make it get passed to your code (index.php)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?imeto_tam=([^&]+)(&.*)?$ [NC]
RewriteRule ^video/view/search/(index\.php)?$ /video/view/search/%2/? [R=301,L]
RewriteRule ^video/view/search/([^/]+)/$ /video/view/search/index\.php?imeto_tam=$1 [L]

Resources