url rewrite to cut folder names - .htaccess

I have some folders where are structures like this
test >main >pages>general(inside pages I have my index.php, contact.php etc)
at the moment my url looks something like this
www.test.com/test/main/pages/general.index.php
how can i rewrite this so i can cut all the folders out and just show
www.test.com/index.php
I have done
RewriteEngine on
AuthUserFile "/home/amytesting/.htpasswds/public_html/passwd"
AuthName "root"
AuthType Basic
require valid-user
Options -Indexes
RewriteCond %{HTTP_HOST} ^test\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com
RewriteRule ^/?$ "http\:\/\/test\.com/\ " [R=301,L]

You can try this simple htaccess code :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.test.com$
RewriteRule ^contact$ http://contactx.test.com/contactX/contact-question.cfm

Related

htaccess redirect subfolder

I would like to redirect the url http://intranet/trac/paradox/report/6 to http://cobra.woking/trac/paradox/report/6. trac is a subfolder and paradox is a subfolder. report/6 are params that need to be kept and may change.
In my apache doc root i have
#/opt/html/.htaccess
Redirect 301 / http://intranet/intranet
RewriteEngine On
RewriteRule ^trac/paradox/report/6$ http://cobra.woking/trac/paradox/report/6 [L,R]
I have tried the following which does not work
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^intranet$
RewriteRule (.*) http://cobra.woking/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^intranet$
RewriteRule (.*) http://cobra.woking/$1 [R=301,L]
URL i want to change is http://intranet/trac/paradox/ to http://cobra.woking/trac/paradox/. I have placed .htaccess in the /opt/html/trac/paradox/.htaccess
In the htaccess file of your intranet's document root, add this to the top of the file:
RewriteEngine On
RewriteRule ^trac/paradox/report/6$ http://cobra.woking/trac/paradox/report/6 [L,R]

Remove directory in htaccess with mod_rewrite

I am trying to strip down
http://host.name/html/about.html
to
http://host.name/about.html
But I keep encountering a 404 error with the following in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
rewritebase /
RewriteRule ^html/(.*) $1 [R,L]
http://htaccess.madewithlove.be/ says its valid, and i think its valid, but there is clearly something I missed.
Can anyone correct me?
Edit:
It also bounces to root when I try to visit /html/
You have it backwards. Since the actual resource is at /html/about.html, you need to internally rewrite to that URI, not from. Once the internal rewrite is in place, you can externally redirect any direct requests to the html directory. So something like:
Options +FollowSymLinks -Multiviews
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /html
RewriteRule ^html/(.*)$ /$1 [L,R=301]
RewriteCond %{DOCUMENT_ROOT}/html%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/html%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/html%{REQUEST_URI} -s
RewriteRule ^(.*)$ /html/$1 [L]
Or you could replace the last set of conditions/rule to:
RewriteCond %{REQUEST_URI} !^/html/
RewriteRule ^(.*)$ /html/$1 [L]

.htaccess redirects to subdomain with %{REQUEST_FILENAME}

We have an online shopping cart that was installed into root - we then decided that we wanted to install a CMS ito root and move the shopping cart to a subdomain.
So we have
domain.com
shop.domain.com
What we are trying to achieve is to redirect URLs that are as follows:
domain.com/product_info.php?products_id=X
To:
shop.domain.com/product_info.php?products_id=X
Where the value of X needs to change as well.
I read (and if I understand this correctly) it would have something to do with %{REQUEST_FILENAME} and so far we have this in our .htaccess, which was a wildcard redirect which is actually redirecting everything to the same file name but we want the value of X to change too.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.co.uk$
RewriteRule ^product_info\.php\/?(.*)$ "http\:\/\/sub.domain\.co.uk\/product_info\.php\?products_id\=1$1" [R=301,L]
The QSA flag will automatically pass the query string back, put this .htaccess on the root folder of your domain:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^product_info\.php$ http://shop.domain.co.uk/product_info.php [R=301,QSA,L]
If the subdomain is also on the same root folder of your domain, then use this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk$ [NC]
RewriteRule ^product_info\.php$ http://shop.domain.co.uk/product_info.php [R=301,QSA,L]
So basically with any of the above rules, if the user access:
domain.com/product_info.php?products_id=4
domain.com/product_info.php?products_id=3
domain.com/product_info.php?products_id=2
domain.com/product_info.php?products_id=1
It will be redirected to:
shop.domain.com/product_info.php?products_id=4
shop.domain.com/product_info.php?products_id=3
shop.domain.com/product_info.php?products_id=2
shop.domain.com/product_info.php?products_id=1
If you in fact need to change the ID this is how you would do it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} products_id=([^&]+) [NC]
RewriteRule ^product_info\.php$ http://shop.domain.co.uk/product_info.php?products_id=1%1 [R=301,L]
And if the domain and sub domain are on the same root folder this is how it would look like:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk$ [NC]
RewriteCond %{QUERY_STRING} products_id=([^&]+) [NC]
RewriteRule ^product_info\.php$ http://shop.domain.co.uk/product_info.php?products_id=1%1 [R=301,L]
Basically you need to use %{QUERY_STRING} to get data from the query string.
So with any of the 2 above rules, if the user access:
domain.com/product_info.php?products_id=4
domain.com/product_info.php?products_id=3
domain.com/product_info.php?products_id=2
domain.com/product_info.php?products_id=1
It will be redirected to:
shop.domain.com/product_info.php?products_id=14
shop.domain.com/product_info.php?products_id=13
shop.domain.com/product_info.php?products_id=12
shop.domain.com/product_info.php?products_id=11

Query string rewriting using .htaccess

i am trying to rewrite a URL for SEO purpose.
The old URL is:
http://www.domain.net/index.php?p=beer
The new URL should be:
http://www.domain.net/beer
My Code in the .htaccess is:
RewriteRule ^([^/\.]+)/?$ index.php?p=$1
Even after hours of research, i have no clue why this is not working :(
Here is the complete .htaccess:
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} Teoma
RewriteRule ^.* - [F]
rewritecond %{HTTP_HOST} !^www\.domain\.net$ [NC]
rewriterule ^(.*)$ http://www\.domain\.net/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^p=uppic$
RewriteRule ^index\.php$ /? [L,R=301]
RewriteRule ^([^/\.]+)/?$ index.php?p=$1
# Pwd service
AuthType Basic
AuthName "Service"
AuthUserFile /xxxx/www/xxxxxx/xxxxx/xxxxxx/.htpasswd
<Files admin.php>
Require user xxxxxxx
</Files>
Options -Indexes
Thanks in advance!
My final question to this code is:
RewriteRule ^([^/\.]+)/?$ index.php?p=$1
Makes working :
http://www.domain.net/beer
and beer is refering to that page:
http://www.domain.net/index.php?p=beer
Which is great! But if i put a / behind beer, e.g.:
http://www.domain.net/beer/
my beer.php file runs at another path, so no css, images, js and so on is included. Any ideas how to fix that without changing the html to http://www.domain.net/style.css ...?
If you want to capture part of the query string, you must use a RewriteCond with QUERY_STRING
RewriteEngine On
RewriteCond %{QUERY_STRING} p=(.+)
RewriteRule ^/?index.php$ /%1? [R,L]
This redirects the client to the new URL http://www.domain.net/beer.
Have you tried this:?
^([^/\.]+)\/?$
Otherwise I would try the .htacces without the other stuff.
Just:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/\.]+)\/?$ index.php?p=$1

.htaccess redirection if file exist

I have a directory structure like this
/ub/
/ub/img/
/ub/inc/
Basically I want to change my img URL to it's parent directory (ub),
When i access http://localhost/ub/mypic.jpg
First, it has to check /ub/img/mypic.jpg, if exist then get that file and pass to the browser, if not then just pass it to http://localhost/ub/index.php?img=mypic.jpg with INTERNAL REDIRECTION.
Second, index.php will create mypic.jpg and store it to /ub/img/, then pass mypic.jpg to the browser.
So, whether the file is exist or not, the browser will only have http://localhost/ub/mypic.jpg as the URL
So far, i have this on my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/ub/img/$1 -f
RewriteRule .+ /ub/img/$1 [L]
I have 2 days to find out the solution, but, it doesn't seem to work,
Thanks,
EDIT :
Hey, I've got this, The request to http://localhost/ub/app/file.php is already passed to index.php?uri=app/file.php as i expected,
but the request to http://localhost/ub/img/mypic.jpg is not passed to index.php?uri=img/mypic.jpg. Is there any solution?
Options -MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /ub/
RewriteCond %{REQUEST_URI} ([^/]+)$
RewriteCond %{DOCUMENT_ROOT}ub/img/%1 -f
RewriteRule ^(.+)$ img/%1 [L]
RewriteCond %{REQUEST_URI} ([^/]+)$
RewriteCond %{DOCUMENT_ROOT}ub/inc/%1 -f
RewriteRule ^(.+)$ inc/%1 [L]
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
Try something like this (place it in .htaccess in the /ub folder):
Options +FollowSymLinks
RewriteEngine on
RewriteBase /ub/
RewriteCond %{REQUEST_URI} ([^/]+\.jpg)$
RewriteCond %{DOCUMENT_ROOT}/ub/img/%1 -f
RewriteRule .+ - [L]
RewriteRule ^(.*)$ index.php?img=$1

Resources