htaccess for url containning specific word - php

I have a website with URLs like /product-category/clothing/accessories/?filter_color=yellow now I want to rewrite these kinds of URLs to /index.php/product-category/clothing/accessories/?filter_color=yellow in fact, I want to add a rewrite rule in my htaccess that if the URL contains product-category and does not contain index.php then rewrite the index.php at the beginning of the URL. For this purpose I have developed the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} product-category
RewriteRule . /index.php [L]
</IfModule>
But it does not work correctly. First, it adds index.php to all of the URLs and then it adds an extra / when /index.php already exists in the URL
Here is the complete .htaccess file:
htaccess

You may use this rule:
RewriteRule ^product-category/.* /index.php/$0 [L,NC]

Related

htaccess redirect url to directory file

am working on making my site urls search engine friendly and for which am rewriting urls with GET parameters and so i have done rewriting but now htaccess is not pointing that url to php file which is suppose to handle the url
my old url
www.domain.com/foo/myfile.php?nid=554
new rewritten url with php
www.domain.com/foo/554-demo-page-title
my current htaccess rules which work for old urls but not for new
Options +FollowSymLinks
RewriteEngine On
#RewriteCond %{SCRIPT_FILENAME} !-d
#RewriteCond %{SCRIPT_FILENAME} !-f
RewriteBase /
RewriteRule ^([0-9]+)-(.*) ./foo/myfile.php?nid=$1 [NC]
so i want to that both old and new urls land on /foo/myfile.php becuase myfile.php can handle both urls incase of old url it rewrite and redirect as new url , i played for few hours with htaccess rules but no success
You can use this rule in site root .htaccess (assuming there is .htaccess inside foo/ directory):
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^foo/(\d+)-.*$ foo/myfile.php [L,QSA,NC]
If you already have /foo/.htaccess then use this rule inside that file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /foo/
RewriteRule ^(\d+)-.*$ myfile.php [L,QSA]

Remove directory name from URL using htaccess

I have bunch of folders and files in folder "front". I want to remove "front" from url. I write htacess but it gives 404 error.
RewriteEngine On
RewriteBase /
RewriteRule ^front/(.*)$ /test/$1 [L,NC,R]
I want to rewrite url like this :
http://localhost/test/front/abc/abc.php
to
http://localhost/test/abc/abc
You have it back to front
RewriteEngine On
RewriteBase /
RewriteRule ^test/(.*)$ /test/front/$1 [L,NC,R]
Try adding this to the htaccess file in your document root, preferably above any rules you may already have there:
RewriteEngine On
RewriteRule ^test/(.*)$ /front/$1 [L,R=301]

How to change the default site directory

I just setup a Wordpress in my server (www.albertotry.esy.es) and I added via FTP a new site files, but if I try to access the site, I don't see the index.html of the new site but the default Wordpress site.
I'm pretty sure there is a problem with the .htaccess file:
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
What should I change to page that will display when I type "www.albertotry.esy.es"?
The .htaccess file you're using is the default WordPress one (except for that first line, which is a duplicate). It sends everything that is not a directory or file to index.php. If you want to load index.html when accessing the domain root only, then change the rule in question and add another after it:
Old Rule:
RewriteRule . /index.php [L]
New Rule:
RewriteRule !^$ /index.php [L]
The !^$ basically means "any string that is not empty" - in this case, the string is the request URI.
Then add a new rule below that one (you can also add it before, if you like - shouldn't make a difference):
RewriteRule ^$ /index.html [L]
Here, the ! is removed, which changes the meaning to "any request URI that is empty. As a result, requesting http://www.albertotry.esy.es/ will show the index.html file, but making any other request would rewrite to WordPress.
However, I believe that WordPress may check the content of the code between the comments # BEGIN and # END, so not sure if this is bulletproof. However, it does answer your question.

.htaccess url rewrite not formatting properly

Currently I working through my localhost in MAMP. I have read and research for the proper way to allow .htaccess file url rewrite and was successful. But now, the file is not formatting the links at all as desired. For example I have three pages index.php, about.php and contact. I am using MVC for the frame working of my site. Is this a problem with the code in the .htaccess file or my localserver?
Currently links look like this when going from one page to another:
localhost/mvc/index.php?
localhost/mvc/index.php?p=about
localhost/mvc/index.php?p=contact
.htaccess should format the links to look like this:
localhost/mvc/index/?
localhost/mvc/about/?
localhost/mvc/contact/?
.htaccess:
<IfModule mod_rewrite.c>
# Turn on the engine:
RewriteEngine on
# Set the base to this directory:
RewriteBase /mvc/
# Redirect certain paths to index.php:
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
</IfModule>
Replace your existing .htaccess code fully with this code:
Options +FollowSymLinks -MultiViews
DirectoryIndex index.php index.html
# Turn mod_rewrite on
RewriteEngine On
# Set the base to this directory:
RewriteBase /mvc/
# Redirect /mvc/index.php?p=page to /mvc/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+mvc/index\.php\?p=([^\s]+) [NC]
RewriteRule ^ %1/? [R=302,L]
# Redirect /mvc/index.php to /mvc/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+mvc/index\.php\s [NC]
RewriteRule ^ /mvc/ [R=302,L]
# Internally forward certain /mvc/page/ to /mvc/index.php?p=page
RewriteRule ^(about|contact|this|that|search)/?$ /mvc/index.php?p=$1 [L,QSA,NC]
Replace
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
with :
RewriteRule ^(about|contact|this|that|search)/\?$ index.php?p=$1
I just escaped the ? with a slash because, otherwise, you don't do this, the question mark will be interpreted and will means that the slash before it is optinal.

How does Wordpress rewrite the URL without a rewrite map?

The .htaccess file for a wordpress site looks something 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>
...and there is no rewrite map set in htdocs. How does this work? How does Apache know how to rewrite these url?
The Apache does not know. All the requests are sent to index.php and Wordpress keeps an internal log of which page to redirect where, and it redirects it. So, in essence, Wordpress actually has two sets of rewrite rules, one internally and a "greedy" external rule in your .htaccess which basically makes all requests refer to the internal rewrite rules.
You may be interested in using this plugin which shows all the internal rewrites that Wordpress is doing itself.

Categories