htaccess rewriterule and redirect not working - apache

I have a particular scenario: the old site urls were made this way: http://www.mysite.it/it/my-old-link
The new one are made this way: http://www.mysite.it/vini.php?famiglia=my-new-link
I would like to the link http://www.mysite.it/it/my-old-link to point to http://www.mysite.it/vini.php?famiglia=my-new-link
This is to preserve SEO position. So, when someone click on search result http://www.mysite.it/it/my-old-link will be redirected to http://www.mysite.it/vini.php?famiglia=my-new-link
But I cannot find a way to do this editing the .htaccess file.
I've set a rewrite rule and a redirect but it doesn't work; here an example of the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
RewriteRule ^/?([^/d]+)/?$ /vini.php?famiglia=$1 [L,QSA]
Redirect 302 http://www.mysite.it/it/my-old-link http://www.mysite.it/my-new-link
ErrorDocument 404 http://www.mysite.it/index.php
But it doesn't work: if I enter http://www.mysite.it/it/my-old-link in the browser I get the 404 page error (index.php)
However: if I enter http://www.mysite.it/my-new-link on the browser I get the correct page.
Is there a way to solve this problem?
TIA tony

In case you are looking for specific rule as per shown samples then try following.
RewriteEngine ON
RewriteRule ^it/my-old-link/?$ vini.php?famiglia=my-new-link [NC,L]
In case you are looking for Generic rules, where if a URI is NOT present as a file or directory then try following. Please make sure either you put above or following rules only at a time.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ vini.php?famiglia=my-new-link [NC,L]

Related

Rewriting URLs with htaccess, multiple parameters

I'm trying to rewrite something like this:
https://mywebsite.com/pages/article.html?id=1&title=Title-Goes-Here
into
https://mywebsite.com/pages/article/1/Title-Goes-Here
Using this Rewrite Rule
RewriteEngine on
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+)$ article.html?id=$1&title=$2 [NC,L]
However, when I try this code in https://htaccess.madewithlove.com/ it gives me
This rule was not met.
Also tried it on my website htaccess file with no result. I don't know where is the problem.
Please don't create OR test rules on online sites, they are NOT trust worthy, so kindly test these rules into your localhost OR apache.
With your shown samples/attempts, please try following htaccess rules. Considering that you are hitting URL https://mywebsite.com/pages/article.html?id=1&title=Title-Goes-Here in browser AND you want to redirect it to URL https://mywebsite.com/pages/article/1/Title-Goes-Here in browser.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect in browser rules here....
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/([^/]*)/([^.]*)\.html\?id=([^&]*)&title=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
##Internal rewrite to html file rules here....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1/$2.html?id=$3&title=$4 [QSA,NC,L]

htaccess - ReWrite one rule to another rule (virtual folder)

I have a file in root directory named propertyid.php
I have a RerwiteRule that works OK:
RewriteRule for-sale/(.*) propertyid.php?pid=$1
User enters this URL and same URL displays in address bar
domain.com/for-sale/(pid)
I want a second rule that will allow users to enter this url
domain.com/sell/(pid)
but I want the first URL to display in the address bar
domain.com/for-sale/(pid)
I have tried multiple variations of the following
RewriteRule ^sell$ /for-sale/propertyid.php?pid=$1
RewriteRule sell/(.*) for-sale?pid=$1
You need to redirect /sell/(.*) to /for-sale/(.*). Your redirect rule of /for-sale/(.*) to propertyid.php?pid=$1 will take care of the rest.
Just check few things:
/sell/(.*) to /for-sale/(.*) rule should come above
/for-sale/(.*) to propertyid.php?pid=$1 redirect rule and should
return 301 http code for moved permanently.
Check for multiple redirects.
Your .htaccess should look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sell(.*)$ http://%{HTTP_HOST}/for-sale$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^for-sale/(.*)$ propertyid.php?pid=$1 [L]

URL rewrite doesn't rewrite page

I tried to use th URL Rewrite module, but it won't rewrite the URL for some reason. If I go to this website: http://localhost/projectredrum/foto.php it will show the correct page, but it doesn't rewrite the URL to what I want.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /projectredrum/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^foto\.php$ /Aquaria-Foto/Fotos
</IfModule>
Rewrite Module Tutorial
After looking at the tutorial mentioned above I figured I should try without
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Because if you use this, it will only rewrite the URL when the URL doesn't match a file name or directory.
However when I did that I got a 404 page not found issue.
Does anyone know why the URL rewrite doesn't work?
Apache does local rewrite, because page is in same server it can load it on same request. Add [R,L] to end of RewriteRule line to rediret browser to wanted address.
R means temporally Redirect,
L means last rule.
R=301 is permanent redirect
eg [R=301,L] does permanent redirect and stops checking other rules.
In your case, you probably want to use this kind line:
RewriteRule ^foto\.php$ /Aquaria-Foto/Fotos [R,L]
Here is more info about flags: http://httpd.apache.org/docs/2.4/rewrite/flags.html
It is not working because of this condition:
RewriteCond %{REQUEST_FILENAME} !-f
since http://localhost/projectredrum/foto.php is a valid file.
To fix the rule you can use this in /projectredrum/.htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /projectredrum/
RewriteCond %{THE_REQUEST} /foto\.php[?\s] [NC]
RewriteRule ^ /Aquaria-Foto/Fotos [L,NC,R=302]
R=302 is used to actually redirect the URL in browser.
In DocumentRoot/.htaccess you can use:
RewriteEngine On
RewriteRule ^Aquaria-Foto/Fotos/?$ /projectredrum/foto.php [L,NC]
References:
Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
Apache mod_rewrite In-Depth Details

mod_rewrite rule for replacing string in URL

I would like to replace a string from old URL:
http://www.homepage.com/projectname/public/ActionName.action
to
http://www.homepage.com/ActionName.action
or even
http://www.homepage.com/short
I got to this point (trying to it get shorten to http://www.homepage.com/ActionName.action)
RewriteEngine On
RewriteRule ^/projectname/public/(.*)$ http://www.homepage.com/$1 [R=301,L]
But I get a 404 error:
The requested resource (/projectnameActionName.action) is not available.
I think you're looking for an internal redirection.
So if you want to be able to put URL's like this in the browser:
http://www.homepage.com/ActionName.action
But show a page like this:
http://www.homepage.com/projectname/public/ActionName.action
Then use this code in your.htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) http://www.homepage.com/projetname/public/$1 [NC,L]
Of course replace the domain name and folders to yours.

Problem in redirection through .htaccess

I have a php generated file like:
http://www.domain.com/?mod=test&act=view
And I want to create a redirection from that address to something like:
http://www.domain.com/view-test
so that everytime a user (or bot) accesses the first uri it gets redirected to http://www.domain.com/view-test viewing the content of the first uri.
Right now I have the following rules under my .htaccess:
RewriteRule ^view-test$ /?mod=test&act=view [NC]
RewriteCond %{QUERY_STRING} mod=test&act=view
RewriteRule ^(.*)$ /view-test? [R,L]
The first rule creates a "page alias" and works if I delete the other two lines (but doesn't redirect my users as i want to)
After placing the last two rules I end up in a Loop or something and I get a broswer message saying "The page is not redirecting correctly"...
If I remove the first rule I get an 404 error saying /view-test could not be found
Any idea what I'm doing wrong?
I think that regex is better. Full .htaccess file for this situation if your php handler is index.php
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} mod=(\w+)&act=(\w+)
RewriteRule ^$ /%2-%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)-(\w+)$ /index.php?mod=$2&act=$1 [L]
forget about .htaccess it's easier to do it in php,i use it in my site, i created an index.php file to redirect users from example.com to example.com/123
in my index.php i put this
<?php
header('Location: http://example.com/123');
?
>