Search and remove the specific string from url using .htaccess rewrite - .htaccess

Using .htaccess apache rewrite I'm looking to remove subfolder/substring from the my url. For now it looks like:
www.domain.com/store/products.php`
and I want to change it to:
www.domain.com/products.php
How can i do it using .htaccess apache rewrite?

You can do that with the root .htaccess:
RewriteEngine on
RewriteBase /
RewriteRule ^store/(.*) /$1 [R=302,L]
Change [R=302] for [R=301] when test work well.

You need mod_rewrite. A rule will be something like this:
RewriteEngine On
RewriteBase /myapp/
RewriteRule ^/store/products\.php$ /products.php
Or, for many different files, e.g. products.php, places.php, blablabla.php:
RewriteEngine On
RewriteBase /myapp/
RewriteRule ^/store/([a-z]+)\.php$ /$1.php
That depends on what URLs you want to rewrite. You should read the docs.
All-matching .htaccess rule
Also, I'd suggest you to use the following catch all URLs pattern. It will redirect any path to index.php. This is widely used in a lot of CMS systems to give you ability to perform dynamic routing with PHP:
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [L,QS
index.php
<?php
$path = isset($_GET['path']) ? explode('/', $_GET['path']) : array();
// If you navigate to /foo/bar/dead/beef, yur $path var will be like:
// $path == array ( 0 => 'foo', 1 => 'bar', 2 => 'dead', 3 => 'beef' );
?>

Related

Mod_rewrite - redirect old urls to the new ones

I'm looking for a solution to redirect old urls (e. g. aboutme.php) to the new ones (same /about-me).
The problem is: if I'll go to example.com/aboutme.php, the user is not redirected to pretty url (/about-me). Adding R=301 doesn't help - it makes /about-me redirect to aboutme.php.
Here's my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*) http://example.com/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^about-me$ /aboutme.php [L]
RewriteRule ^portfolio$ /portfolio.php [L]
RewriteRule ^motion$ /motion.php [L]
RewriteRule ^contact$ /contact.php [L]
RewriteRule ^contact-thanks$ /contact_thanks.php [L]
</IfModule>
You have to make it in the other direction.
If you want to rewrite aboutme.php to about-me
you should use
RewriteRule ^aboutme.php /about-me [R=301,L]
EDIT:
The place /about-me has to be present. If not, you can redirect this virtual place to a file (say rewrite.php) which gets the actual file from database or some configuration. Place the following rules at the bottom of your htaccess rules.
#don't rewrite the rewrite script
RewriteRule rewrite.php - [L]
#as example don't rewrite the image folder
RewriteRule ^images - [L]
#everything else that doesn't match a rule
RewriteRule ^(.*)$ rewrite.php?file=$1 [QSA,L]
after that you can access your requested virtual file in rewrite.php with $_GET['file'].
EDIT:
your rewrite.php could look like this: (this is the simplest way todo. but you have to do some error checking etc.)
<?php
$file = $_GET['file'];
$config = array(
'about-me' => 'aboutme.php'
);
include($config[$file]);
?>

dynamic to static url AND removing the dynamic

I want to change alle dynamic url's to static ones,
but after rewriting the dynamic url's are still responding/available.
What did I do =>
I found this Tool for SEO:
http://www.webconfs.com/url-rewriting-tool.php
I entered this:
.../filmdetails.html?var=ich_einfach_unverbesserlich_ii
Then I put into my .htaccess this:
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^filmdetails/(.*)/$ filmdetails.html?var=$1
Works, but now I got a problem: This URL is still available and should not be: .../filmdetails.html?var=ich_einfach_unverbesserlich_ii
How do I get rid of the dynamic url's?
Your rule only rewrites the nicer looking URL to the one with a query string. Rules only work from a "pattern" -> "target" way, the mapping won't magically work the other way. You'll have to create a separate rule in order to redirect the browser:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /filmdetails\.html\?var=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /filmdetails/%2/?%3 [L,R=301]
Try:
Options +FollowSymLinks
RewriteEngine On
#set this to path to your filmdetails.html file (from the document root)
RewriteBase /
#checking if redirect already happened
RewriteCond %{ENV:REDIRECT_PASSED} !^$
RewriteRule $ - [L]
#Your rewrite rule
RewriteRule ^filmdetails/(.*)/$ filmdetails.html?var=$1 [L,E=PASSED:1]
#redirecting from filmdetails.html with query string ?var=something
RewriteCond %{QUERY_STRING} ^var=(.+)$ [NC]
RewriteRule ^filmdetails.html$ filmdetails/%1/? [R]
filmdetails.html?var=something will be redirected to filmdetails/something

How to redirect in .htaccess? => /ua/ to /?lang=ua

I have a multilingual website and .htaccess, which displays the all page and language ?lang=ua style.
I want to redirect (using code 301) asks site.com/en/ to site.com/?lang=en with RewriteEngine.
Example:
site.com/en/ => site.com/?lang=en
site.com/ua/news.html => site.com/news.html?lang=ua
site.com/ua/news/2-material-two.html => site.com/news/2-material-two.html?lang=ua
and so on much...
How to prepare Htaccess file for Apache to meet this criterion? And how do?
Thanks in advance
Put this code in your .htaccess file under DOCUMENT_ROOT dir:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]+)/(.*)$ /$2?lang=$1 [L,R=301,QSA]
Try this:
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteBase /
RewriteRule http://site.com/([^/]+)/(.+).html$ http://site.com/$2.html?lang=$1 [R=301,NC]
I've just put this together off the top of my head so it may not be quite right, but the idea is that it will examine the url and the ([^/]+) should read the 1st directory, and then read everything up to the .html.
It then rewrites the url to be site.com/[everything after 1st dir up to .html].html?lang=[name of 1st dir]
Add the following to your htaccess file in the root of your domain
RewriteEngine On
RewriteBase /
#if the lang param is not present
RewriteCond %{QUERY_STRING} !^lang=
#capture the language code and redirec to url with lang param
RewriteRule ^([a-zA-Z]+)/(.+\.html)$ /$2?lang=$1 [L,R=301,NC]
Please, if you must use general "possibly directory matching"-rule, consider the !-d operator (and optional: the !-f operator).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/ http://site.com/?lang=$1 [R=301,L]
So you won't end up fixing all kinds of strange problems with existing files and/or directories.

.htaccess rewrite rule ,last query string is append to the url

Hi i make one cms sites and i need to rewrite my url
current my url is http://www.example.com/index.php?link=pages&cmsid=2&cmsLink=Carpet
it refers the cmsLink
I want my url like http://www.example.com/Carpet
I am using the following code
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^index.php?link=(.*)&cmsid=(.*)&cmsLink=(.*) $3
To achive this url it not directly possible with .htaccess
I use regular expression and other tings in htacess as well
I put remove the cmsid
current my url is http://www.example.com/index.php?link=pages&cmsLink=Carpet
RewriteCond %{REQUEST_URI} !/admin
RewriteCond %{REQUEST_URI} !^/(.*).php
RewriteBase /
RewriteRule ^([^/]+)/?$ index.php?link=pages&cmsLink=$1&%{QUERY_STRING} [L]
it return me http://www.example.com/Carpet
Try changing your last rule to this:
RewriteRule ^(.+)$ /index.php?link=pages&cmsid=2&cmsLink=$1
Since you want to have url like http://www.example.com/Carpet, so cmsid and link in your url has to be hardcoded to 2 and pages.

301 redirect from query string to simple URL

I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.

Resources