301 Redirect for specific url - apache

I want to edit the URL of a page (only one page called meto.php) to a custom URL.
It is important that the meto.php file located in sub-directory /pob/
i want to change this www.domain/pob/meto.php to www.domain/pob/define.php
When visitor type this address www.domain/pob/meto.php address bar show this address: www.domain/pob/define.php
Please note define.php file does not exist.
I tried this code in .htaccess file at /pob/ (not root directory):
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com[nc]
RewriteRule ^(.*)$ http://www.domain.com/pob/$1 [r=301,nc]
Redirect 301 meto.php define.php
//also
Redirect 301 /meto.php http://www.domain.com/pob/define.php
//
RewriteRule ^meto.php define.php
Unfortunately all of the above tries fails.
Server info:
Apache Version 2.2
PHP: 5.5

You can not rewrite your files using Redirect Directiv of mod_alias , it is used for external redirection.
Try mod_rewrite
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/pob/$1 [R=301,L]
#1) redirect "/pob/meto.php to "/pob/define.php"
RewriteCond %{THE_REQUEST} /meto.php [NC]
RewriteRule ^ /define.php [NC,L,R]
#2) rewrite "pob/define.php" to "/pob/meto.php"
RewriteRule ^define\.php$ /pob/meto.php [NC,L]

Related

htaccess redirect for load a specific image url at subdomain from main domain

I have a subdomain (us.example.com) and main domain (example.com). I want to redirect my subdomain url to main domain only for a specific image. For example if I hit http://us.example.com/images/test.jpg then it should redirect to http://example.com/images/test.jpg
I am trying 301 redirect as below but it's not working.
Redirect 301 https://us.example.com/images/test.jpg https://example.com/images/test.jpg
I have tried other rules also but they all work on complete image directory not for a specific image under directory.
Update
Changed rule like this -
RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC]
RewriteRule ^/?images/test.jpg$ https://example.com/images/test.jpg [R=301,L]
But no luck.
The Redirect directive from mod_alias has the following syntax:
Redirect [status] [URL-path] URL
and, as you see, it receives/accepts the URL path, and not the whole URI/URL for matching.
For the subdomain match, use an <if> directive:
<If "%{HTTP_HOST} == 'us.example.com'">
RedirectPermanent /imges/test.jpg https://example.com/images/test.jpg
</If>
The same can be achieved using mod_rewrite as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC]
RewriteRule ^/?images/test.jpg$ https://example.com/images/test.jpg [R=301,L]
Try this mate ,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^us.domain.com$ [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
</IfModule>
What will happen for the above .htaccess was ,
If you visit
us.domain.com/somelink
It will redirect to
domain.com/somelink
EDIT :
Suppose if the above doesn't help try the one below with symlinks
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^us.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
Update 1 :
For only test.jpg
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^us.domain.com/images/test.jpg [NC]
RewriteRule (.*) http://www.domain.com/images/test.jpg [R=301,L]
</IfModule>

.htaccess redirect root url exclude index.php

I want to redirect the root URL only excluding index.php which is in root folder. This is what i have so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} website\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://website.com/redirect/ [L,R=301]
RewriteRule ^/index\.php$ - [L]
It still redirects the index.php file when manually entered!
Please try the following:
RewriteEngine on
# Condition: Only match host 'website.com'
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
# Rule: If request is for root, redirect
RewriteRule ^$ http://website.com/redirect/ [R=302,L]
As you used 301 already, you may need to clear the cache for the browser to redirect properly.
Once you are happy, and would like to make the redirect permanent, change 302 to 301.

mod_rewrite keeps adding .html for files

I have a redirect situation where the site is part dynamic and part generated .html files.
For example, mysite.com/homepage and mysite.com/products/42 are actually static html files
Whereas other URLs are dynamically generated, like mysite.com/cart
Both mysite.com and www.mysite.com are pointing to the same place. However I want to redirect all of the traffic from mysite.com to www.mysite.com.
I'm so close but I'm running into an issue where Apache is adding .html to the end of my URLs for anything where a static .html file exists - which I don't want.
I want to redirect this:
http://mysite.com/products/42
To this:
http://www.mysite.com/products/42
But Apache is making it this, instead (because 42.html is an actual html file):
http://www.mysite.com/products/42.html
I don't want that - I want it to redirect to www.mysite.com/products/42
Here's what I started with:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
I tried making the parameters and the .html optional, but the .html is still getting added on the redirect:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)?(\.html)?$ http://www.mysite.com/$1 [R=301,L]
What am I doing wrong? Really appreciate it :)
Here is the code you will need in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## first add www to your domain for and hide .html extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ http://www.mysite.com%1 [R=301,L]
## add www to your domain for other URIs without .html extension
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^ http://www.mysite.com%{REQUEST_URI} [R=301,L]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f [NC]
RewriteRule ^ %{REQUEST_URI}.html [L]
Maybe you should try looking at apache's mod_negotiation to get rid of the .html or any file extension?
Link: http://httpd.apache.org/docs/2.0/mod/mod_negotiation.html

redirect to subfolder

I have set up url redirect on my host--but its not working.
my htaccess looks like this now
rewriteengine on
rewritecond %{HTTP_HOST} ^.*$
rewriterule ^http:\/\/www\.bangkoksoftball\.info "http\:\/\/www\.bangkoksoftball\.info\/wordpress" [R=301,L] #4d3a8d4534e567
what i want is any request to http://www.bangkoksoftball.info to be redirected to http://www.bangkoksoftball.info/wordpress/
but any request to a path file or directory off the root not to be redirected
this so people can still access the old site via /home.html or index.html etc.. and still be able to navigate outside the wordpress folder
Try this in your .htaccess file:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^(www\.)?bangkoksoftball\.info$ [NC]
RewriteRule ^/?$ /wordpress/ [R,L,NE]
Remember there is NO presence of http/https in %{HTTP_HOST} or %{REQUEST_URI} variables. Also . needs to be escaped in domain match. NE flag is for not escaping the query parameters.
The rewriterule line does not look at the domain, you must specify the domain the rule applies to in the RewriteCond line. In the rewriterule line, you specify first the paths that should trigger the rule, and the path the user should be sent to instead.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^http:\/\/www\.bangkoksoftball\.info$
RewriteRule ^/$ "http\:\/\/www\.bangkoksoftball\.info\/wordpress" [R=301,L]
This will send only requests to www.bangkoksoftball.info/ to the wordpress folder. If you want to also redirect www.bangkoksoftball.info/index.php to the wordpress folder, you would need to add an additional set of directives:
RewriteCond %{HTTP_HOST} ^http:\/\/www\.bangkoksoftball\.info$
RewriteRule ^/index\.php$ "http\:\/\/www\.bangkoksoftball\.info\/wordpress" [R=301,L]
This should work for you as it worked here:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?bangkoksoftball.info$ [NC]
RewriteRule ^/$ http://www.bangkoksoftball.info/wordpress/$1 [R=301,L]

redirect into subdirectory AND out of subdirectory

i have a mod_rewrite redirection problem i cannot figure out.
all requests from a specific domain get "silently" rewritten into a designated subdirectory. e.g. www.mydomain.net/hello.html retrieves the file in /net/hello.html. the following .htaccess (placed in my hosting root) achieves this perfectly:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200 # <-- i will need this later. read to the end of the post.
RewriteRule .* - [L]
rewriteCond %{HTTP_HOST} ^www.mydomain.net$
rewriteCond %{REQUEST_URI} !^/net.*$
rewriteRule (.*) /net/$1 [L]
however, direct URLs into this directory however should visibly redirect with a 301 to the URL without that subdirectory. e.g. www.mydomain.net/net/hello.html should redirect to www.mydomain.net/hello.html (which than still retrieves the file in /net/hello.html). my .htacces file for this (placed in /net) unfortunately doesn't work:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^(.*) /$1 [R=301,L]
i get an infinitive redirect loop despite the RewriteCond %{ENV:REDIRECT_STATUS} 200 block in the root .htaccess file... so what's wrong?
btw, i have to use mod_rewrite, because the site is externaly hosted and i have no access to the apache configs.
many thanks for any pointers.
Inspect the HTTP request line in THE_REQUEST instead:
RewriteCond %{THE_REQUEST} ^GET\ /net[/? ]
RewriteRule ^net($|/(.*)) /$2 [L,R=301]