Rewrite without changing URL in same domain using htaccess - php

I have a Two URLs
NEW URL :
http://host.ip.address/nl/business/producten-en-diensten/gsm-en-smartphones/samsung-galaxy-j1-2016-zwart
OLD URL :
http://host.ip.address/nl/producten-en-diensten/gsm-en-smartphones/samsung-galaxy-j1-2016-zwart
when in request NEW URL from browser it should retrieve content from OLD URL and the URL should not change in browser.
I have tried the below rule in .htaccess it is rewriting and redirecting to the OLD URL which should not do in my case
RewriteEngine on
RewriteRule (^|.*?/)nl/business/(.*)$ /$1nl/$2 [L,R=301]

It is not possible to do a redirect and keep the original URL in the address bar." This is because a redirect, by definition, involves telling the browser to ask for the requested resource at a different URL. So the browser updates its address bar and uses the new URL.
The simplest way would be use a rewrite, not a redirect. An internal rewrite simply serves content from a local file-path that is different from the requested local URL-path.
You can use something easy like this.
RewriteEngine On
RewriteRule nl/producten-en-diensten/gsm-en-smartphones/samsung-galaxy-j1-2016-zwart nl/business/producten-en-diensten/gsm-en-smartphones/samsung-galaxy-j1-2016-zwart
You may need to also add
Options +FollowSymLinks

Related

Magento htaccess redirection issue

I was trying to redirect a particular URL and all its child URL's to a particular URL. Using 301 redirection this is working fine but the browser URL is showing the test contents also. Please see below for more details
Example: https://www.mywebsite.com/customer/account/login/referer/* needs to be redirected towards https://www.mywebsite.com/customer/account/login/register
What I tried is:
Options +FollowSymLinks
RewriteEngine on
Redirect 301 /customer/account/login/referer https://mywebsite.com/customer/account/login/register
The htaccess rule is working fine for just this URL. If I type
https://mywebsite.com/customer/account/login/referer/testing
This will be redirected towards https://mywebsite.com/customer/account/login/register/test
Please let me know how I will be able to trim the other part (URL part after register/*)from the redirected URL. What I am trying to achieve is
https://mywebsite.com/customer/account/login/referer/* [ANY URL's AFTER referer/ (Including /referer) needs to be redirected to https://mywebsite.com/customer/account/login/register].
This is expected behaviour, as stated in the documentation of Redirect
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
To redirect all requests starting with this prefix to the given URL, you may use RedirectMatch, which uses regular expressions instead of a URL prefix
RedirectMatch ^/customer/account/login/referer https://mywebsite.com/customer/account/login/register
You don't need to use
Options +FollowSymLinks
RewriteEngine on
because this is for mod_rewrite. Redirect and RedirectMatch are provided by the mod_alias module.

htaccess url rewrite but executes the same page

i have an url like
http://example.com/folder1/folder2/admin_login.php
the above is active page. but i want to show the below url instead of above but functioning the same page.
http://example.com/folder1/folder2/login
So the question is display url-2 in browser but exeuctes url-1
i have tried this code in htaccess file
RewriteEngine on
RewriteRule ^folder1/folder2/admin_login.php$ http://example.com/folder1/folder2/login [NC,L]
When i put url-1 in browser address bar. it will convert into the url-2 but shows Page Not Found
You have it turned around. You want the second url to be in the address bar. That means you have to link to that url. When a request with that url comes to the server, the server has to internally translate that url to a working url. Besides that, you should not have the full url as a rewrite target, because this causes the rule to function as a redirect instead of an internal rewrite.
The following rule should do the trick:
RewriteRule ^folder1/folder2/login/?$ /folder1/folder2/admin_login.php [L]

Htaccess wrongly making 301 redirect

I'm just trying to display css content at http://mydomain.com/dynamic_css/presets/
when user's browser loads http://mydomain.com/css/dynamic.css using the following
RewriteEngine on
RewriteRule ^dynamic\.css$ http://mydomain.com/dynamic_css/presets/ [QSA,L]
But instead browser gets http 301 redirected to http://mydomain.com/dynamic_css/presets/
Any idea why ?
Basically, the script at http://mydomain.com/dynamic_css/presets/ shows up a CSS generated code to allow more styles control from admin settings.
You can't specify the full domain in your request. mod_rewrite assumes this is an external URL which can only be handled via redirect. Try the below instead.
RewriteRule ^dynamic\.css$ /dynamic_css/presets/ [QSA,L]

How to add a custom word to my domain URL for every request?

Consider my domain name is
www.mydomain.com
Consider a page request
www.mydomain.com/user/register
I want to add a custom word after base URL for every request inside mydomain.com.example
www.mydomain.com/customword/
www.mydomain.com/customword/user/register
Can we do this using URL rewriting in htaccess file ?
Actually it should execute 'www.mydomain.com/user/register' internally...but externally the URL should look like www.mydomain.com/customword/user/register.
You could create the directory "register", and put an index file inside it that performs the action.
That's probably the simplest way without url rewriting anyway.
UPDATE (since the question was updated)
In .htaccess:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-.]+)/user/register/?$ user/register.php?customword=$1
register.php will receive a GET request:
//User went to www.mydomain/word/user/register
echo $_GET['customword']; // will return word in this case
Make sure that you have mod_rewrite enabled :)
Yes, you can do it with htaccess
Here is an example which will add a trailing slash with url if it doesnt contain trailing slash
http://enarion.net/web/htaccess/trailing-slash/
edit formatting updated
If you are serving one site from this then the following should work:
Edit your .htaccess file to do a url rewrite
accessing www.yourdomain.com/user/registry will actually server content from www.yourdomain.com/customword/user/registry
RewriteEngine On<br>
RewriteCond %{REQUEST_URI} !^/customword/<br>
RewriteRule ^(.*)$ /customword/$1
You haven't mentioned what kind of site you;re using..eg: PHP, MVC etc as you could do similar thing in there as well.

How can I forward a dynamic URL to twitter using .htaccess or PHP?

I need to rewrite a dynamic URL. The content myurl and mytext is always different and should be insert in the "text" and "url" from the Twitter string.
This is the string:
http://example.com/share/?myurl=http%3A%2F%2Fwww.example.com&mytext=helloworld
/* forward to: */
http://twitter.com/intent/tweet?related=Example%3Aname&text=helloworld&url=http%3A%2F%2Fwww.example.com&via=example
How can that be made? (I browsed the .htaccess suggestions but couldn't find a solution for my specific problem.)
Does your server support PHP?
You could just put something like the following in a PHP file:
Header("Location: http://twitter.com/intent/tweet?related=Example%3Aname&text=$_GET['mytext']&url=$_GET['myurl']&via=example");
It will be more efficient to handle it with rewrite Rules.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^myurl=(.*)&mytext=(.*)$
RewriteRule ^(.*)$ http://twitter.com/intent/tweet?related=Example%3Aname&text=%2&url=%1&via=example [R,L]
If you want this to be permanent redirect then replace R with R=301.
With permanent redirects , these links will always be redirected by browser and your server will have to deal with less traffic if that is desired.

Categories