URL Rewriting in. Htaccess - .htaccess

I have a website on a test server and I want to rewrite URL for this website because it is very long
I wish our visitors instead of entering this URL:
http://staging.company.fr/site2.it/s...oject2/public/
enter this URL:
www.monsite.com
I created a file. htaccess:
RewriteEngine On
RewriteRule ^/~(.+) http://www.monsite.com/~$1 [NC,L]
but does not work

While in .htaccess mod_rewrite doesn't match leading slash in a URL since it is applied per directory. Therefore following should work:
RewriteEngine On
RewriteRule ^(.+)$ http://www.monsite.com/$1 [L,R=301,NE]
This will redirect every URL in your existing domain other than home / to monsite.com
Reference: Apache mod_rewrite Introduction

Related

Force redirect to /index.php/url in Joomla htaccess

I have a Joomla site with .htaccess URL rewrite. So in Google my links appear as:
DOMAIN/abc ...
Now probably the rewrite mod on the hosting stopped working and I need to redirect all people from Google to link such as:
DOMAIN/index.php/abc ...
I tried this
RewriteEngine On
RewriteRule ^(.*)$ https://example.com/index.php/$1 [R=301,L]
but it doesn't work.
You should exclude the request start with index to avoid looping so , change the rules as follow:
RewriteEngine On
RewriteRule !^index\.php https://example.com/index.php%{REQUEST_URI} [R=301,L]
Note: clear browser cache

Redirect specific URL to another URL on another subdomain

I have 5 URLs on a subdomain website http://subdomain.example.com, and I want them to redirect to 5 other URLs on my main website, https://www.example.com/.
Important: URLs do NOT have the same structure!
Example:
http://subdomain.example.com/url1 should redirect to https://www.example.com/ipsum
http://subdomain.example.com/url2 should redirect to https://www.example.com/lorem
etc.
How can I handle that?
UPDATE:
There is a play folder (name of the subdomain) which contains the subdomain website files and a htdocs folder which contains the www website files.
Here is the .htaccess file in my play folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Since the subdomain has it's own .htaccess file, you don't need to specify the hostname as part of the redirect. And since you already have mod_rewrite directives in the subdomain's .htaccess file, you should also use mod_rewrite for these redirects (to avoid conflicts). Otherwise, you'll need to specify these redirects one-by-one.
Try the following at the top of your subdomain's /play/.htaccess file. Note that this needs to go before the existing directives in the file.
# Specific redirects
RewriteRule ^url1$ https://www.example.com/ipsum [R=302,L]
RewriteRule ^url2$ https://www.example.com/lorem [R=302,L]
The above would match a request for http://subdomain.example.com/url1 and redirect accordingly, etc.
Note that the RewriteRule pattern (regular expression) does not start with a slash when used in a per-directory (.htaccess) context.
Note that these are 302 (temporary) redirects. Change them to 301 (permanent) - if that is the intention - only once you have confirmed they are working OK (to avoid caching issues).
Try this...
Redirect 301 http://subdomain.example.com/url1 https://www.example.com/ipsum
Redirect 301 http://subdomain.example.com/url2 https://www.example.com/lorem

301 redirect: redirect urls with .php to folder

In .htaccess I want to redirect urls like this:
/products.php/a7-frames
/products.php/a6-frames
to this:
/picture-frames/a7-frames
/picture-frames/a6-frames
So need to substitute products.php with picture-frames.
After a lot of Googling I tried this:
RewriteBase /
RedirectMatch 301 (.*)\.php/?$ https://www.domainname.com/picture-frames$1
But it doesnt work, if I enter this url: /products.php/a7-frames the browser says there are too many redirects and goes to:
/picture-frames/index
It's substituting the products.php for picture-frames which is great, but I'm not sure why it adds "index" on the end rather than the /a7-frames part of the url? How can I fix this?
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+products\.php/([^\s?]+) [NC]
RewriteRule ^ /picture-frames/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^picture-frames/([^/]+)/?$ products.php/$1 [L,NC]
Make sure to clear your browser cache before testing this change.

.htaccess redirect based on a certain url param that redirects to another site

I'm trying to set up a .htaccess redirect based on a certain url param, which then redirects to another site.
Example:
Original URL: www.domain.com/?param=true
Redirected URL: www.newdomain.com
I've tried many different approaches, but nothing seems to be working.
This rule should work from site root .htaccess of www.domain.com:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?param=true[&\s]
RewriteRule ^ http://newdomain.com/? [L,R=301]

How to create permalink in htaccess

I want to redirect a link to another with .htaccess file in Linux host. Can you help me?
from: http://example.com/examp
to: http://example.com/examp.php
And another one for my other site
from: http://example.com/examp
to: http://example.com/user.php?u=examp
You will need mod_rewrite enabled for this. Start with placing these lines into .htaccess:
RewriteEngine On
RewriteBase /
TBH I'm not 100% sure what do you mean exactly by permalink and how do you want to redirect, so I will provide 2 variants for each URL: rewrite (internal redirect) and redirect (301 Permanent Redirect).
1. This will rewrite (internal redirect) request for http://example.com/examp to http://example.com/examp.php while URL will remain unchanged in browser:
RewriteRule ^examp$ examp.php [L]
2. This will do the same as above but with proper redirect (301 Permanent Redirect) when URL will change in browser:
RewriteRule ^examp$ http://example.com/examp.php [R=301,L]
3. This will rewrite (internal redirect) request for http://example.com/examp to http://example.com/user.php?u=examp while URL will remain unchanged in browser:
RewriteRule ^examp$ user.php?u=examp [QSA,L]
4. This will do the same as above but with proper redirect (301 Permanent Redirect) when URL will change in browser:
RewriteRule ^examp$ http://example.com/user.php?u=examp [QSA,R=301,L]
Useful link: http://httpd.apache.org/docs/current/rewrite/
You will need mod_rewrite enabled for this
from: http://example.com/123
to: http://example.com/index.php?q=123
RewriteEngine on
RewriteBase /
RewriteRule ^/?([-A-Za-z0-9]+)/?$ index.php?q=$1 [QSA,L]
You'll want to look at RewriteRules and know/understand regular expressions. It'll be something like this:
RewriteEngine on
RewriteRule ^(.*)\/examp$ /examp.php [R=301,L]
- and -
RewriteRule ^(.*)\/[a-zA-Z0-9\-\_\.]+$ /user.php?u=$1 [R=301,L]
The latter example will take what's in-between the [] and place it in the $1 variable
Here is a good link to get you started:
http://www.webweaver.nu/html-tips/web-redirection.shtml

Resources