mod_rewrite rule for replacing string in URL - apache

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.

Related

htaccess rewriterule and redirect not working

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]

RewriteRule doesn't rewrite correctly

I'm hosting my development site on the localhost server and I used to access it at 127.0.0.1/dev. In the folder I have a .htaccess file containing following information. I'm new with the RewriteEngine and don't get this work.
RewriteEngine on
RewriteRule ^/(.*)$ /index.php?page=$1
When I'm trying to access 127.0.0.1/dev/home, I simply get a message that the page doesn't found. I can't see where rewrite is redirecting me, so I can't debug the problem in easy way. I think that you can see the problem at the first look.
Thanks in advance.
Try this rule in /dev/.htaccess without leading slash in source pattern and target URL:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?page=$0 [L,QSA]

404 error in url rewriting to get user-friendly urls

I have a static html website on a shared hosting.
I would like to turn these html files to user-friendly urls.
IE: website.com/web-design.html becomes website.com/web-design/
I am using these rules so that the corresponding html file is served when I request website.com/web-design/:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html
It works if I type in the browser website.com/web-design (without trailing slash), but instead with website.com/web-design/ I get a 404 error:
The requested URL /web-design.html/ was not found on this server.
I want both urls (with and without trailing slash) to work...
It seems that a slash is added at the end of the html file, giving a 404.
I can't figure out how to fix it...
Thanks for your help.
You should try your rule like this
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /$1.html [L]

URL rewriting by htaccess

I would like to rewrite URLs using htaccess like, in the example below.
My HTTP request is:
http://example.com/store1/index.php?page=user/test
I would like to get the URL in browser as:
http://example.com/store1/user/test.**
Also, please note, that the 1st parameter followed by the slashes after example.com is not a real folder; so I am already rewriting it, using this htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+[^/](.*)$ /$1 [QSA]
and it works.
But I need to achive the˛htaccess rewriting, without affecting the above settings.

URL rewrite rule that allows long URL to appear shorter

I would like to users to visit:
domain.com/example
and have the content from the following page displayed:
domain.com/directory1/directory2/directory3/example.html
In the browser, I would like it to say domain.com/example without redirecting. We have a series of landing pages that need to have short URLs within the domain.
I considered doing something programmatically such as a PHP include however I felt an .htaccess rewrite rule would be best practice.
So you want:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^example$ /directory1/directory2/directory3/example.html [L]
If you want it to work for every file (replacing "example" with any file) then replace the RewriteRule line with:
RewriteRule ^([^/]+)$ /directory1/directory2/directory3/$1.html [L]