I am new to htaccess file and have the following requirement.
I have a dynamic page which handles like this
com/index.php?postname=products
.com/index.php?cat=news&country=india
for these pages I want SEO friendly urls like this
.com/products/ to .com/index.php?postname=products
.com/news/country to .com/index.php?cat=news&country=india
What code do I need to put it in htaccess file?
Do I need to write RewriteCond or RewriteRule or both?
If possible please give me the sample code to do this.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?postname=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?cat=$1&country=$2 [L]
Try this Example for your work.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^postname=(.*)$
RewriteRule ^index\.php$ /postname/%1? [R=301]
RewriteRule ^postname/([^-]+)$ /postname.php?id=$1 [L]
The above code will redirect
index.php?postname=products
to
index/products
Here is the htaccess code that helps you
Save the line .htaccess in your index.php folder or root folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([A-Za-z0-9_\-]+)$ index.php?postname=$1 [L]
RewriteRule ^([A-Za-z0-9_\-]+)/([A-Za-z0-9_\-]+)$ index.php?cat=$1&country=$2 [L]
</IfModule>
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
<IfModule mod_gzip.c>
mod_gzip_on No
</IfModule>
It works fine for me.
Related
I have multiple php pages like this:
http://www.example.com/si/article.php?id=122&nTitle=my-title
http://www.example.com/si/post.php?id=352&pTitle=my-post-title
i would like to rewrite them to
http://www.example.com/si/122/my-title
http://www.example.com/si/352/my-post-title
I tried different things in my .htaccess file and no result. Please tell me how to make to work with multiple rules. If I use one rule in .htaccess file its work for one php page like below:
RewriteRule ^([\s\w-]+)/(.*)/?$ article.php?id=$1&nTitle=$2 [L,QSA,NC]
This is now I tried it for multiple pages:
<ifModule mod_rewrite.c>
Options -Indexes
RewriteEngine on
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^/([\s\w-]+)/(.*)$ /article.php?id=$1&pName=$2 [L,QSA,NC]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^/([\s\w-]+)/(.*)$ /post.php?id=$1&pName=$2 [L,QSA,NC]
</IfModule>
Can anyone give me an advice on how to use this functionalities in .htaccess?
Thank you.
You can use url like : (look like different from each other)
http://www.example.com/arical-si/122/my-title
http://www.example.com/post-si/352/my-post-title
then redirect to :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$0
RewriteRule ^artical-si/([0-9+/=$]*)/([A-Za-z0-9+/=$]*)$ article.php?id=$1&nTitle=$2 [L]
RewriteRule ^post-si/([0-9+/=$]*)/([A-Za-z0-9+/=$]*)$ post.php?id=$1&nTitle=$2 [L]
</IfModule>
First of all you should test your regex, you can do it there regex generator
Then in your htaccess, you can put your regex like this:
RewriteEngine on
RewriteRule ^/post.php?id=([0-9])&pName=([a-z]+)$ post/$1/$2 [L]
RewriteRule ^/article.php?id=([0-9])&pName=([a-z]+)$ article/$1/$2 [L]
try it step by step and you can debug it easily.
I'm trying to build a shopping cart using PHP & MySQL. Right now I'm in the process of creating product-detail pages. So this is how I set up my htaccess (located in the root folder):
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
My problem is I can only access a certain page if index.php is included. For example, http://localhost/thegamingplace/products/details/1 displays a "Not Found" error, but http://localhost/thegamingplace/index.php/products/details/1 works.
Can someone take a look at my htaccess and tell me what I'm doing wrong?
Try this:
RewriteCond %{REQUEST_URI} !^(index\.php|resources|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ index.php/$1 [NC,L]
It works when tested at http://htaccess.mwl.be/
If it doesn't work make sure you have mod_rewrite installed and enabled.
i have a problem with the htaccess file from a system which i bought.
The Original 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>
My modified version looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^city-([^/]+).html searcharea?city=$1&sd=ls [L]
RewriteRule ^city-normal-([^/]+).html searcharea?city=$1&sd=pb [L]
RewriteRule ^city-special-([^/]+).html searcharea?city=$1&sd=oeb [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The Systems Base Home Link looks like this:
http://www.url.de/store/home
and my .htaccess is completely ignored, i wonder why?
i want to include a seo linking to the system and reffer its url to the function but it doesnt work with this htaccess.
i have this htaccess rules in my other htaccess file from another system and there it works perfectly.
edit:
the rules from original file work, just my rules are ignored. mod_rewrite is enabled
Structure of my dirs:
/
/.htaccess
/index.php -> uses yiiframework to build pages
/core
/core/css
/core/js
This is because your orignal htaccess rules are conflicting with the new rules and they rewrite all non existent requests to index.php. To fix this, you need to reorder your rules .
Try :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^city-([^/]+).html searcharea?city=$1&sd=ls [L]
RewriteRule ^city-normal-([^/]+).html searcharea?city=$1&sd=pb [L]
RewriteRule ^city-special-([^/]+).html searcharea?city=$1&sd=oeb [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Make sure that mod_rewrite is enabled in apache on your server.
I am working on a new script, I am using mod_rewrite to create permanent links. But I also want to have a login page that goes to a different main page.
ie.
http://www.example.com/dashboard
will load index.php
but
http://www.example.com/login will load login.php
currently my .htaccess 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>
Not sure the best way to do what I would like. Tried to look around and couldn't find this example.
I think that all You need is just .php rewrite, so that You can use php pages with or without .php extension. The rest (redirecting) You should do from php scripts.
Here is how .htaccess should look like in this case.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [QSA,L]
</IfModule>
i got a problem with rewriting my edit.php?id=1 to edit/id/1. Right now i have the following in my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^edit/id/([^/.]+)$ edit.php?id=$1 [NC]
This doesn't change the url. Can something see what i do wrong?
You need one additional rule to change the URL externally. This should be placed in root .htaccess:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/([^/]+)/edit\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1/edit/id/%2? [R=302,L,NE]
RewriteRule ^([^/]+)/edit/id/([^/.]+)$ $1/edit.php?id=$2 [NC,L,QSA]
Give this a try and see how it works for you.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/edit/id/([^/.]+)$ $1/edit.php?id=$2 [NC,L]
</IfModule>
OR you can do this if you only have a couple of directories the same. The will go in the root .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(profiles|customers|test)/edit/id/([^/.]+)$ $1/edit.php?id=$2 [NC,L]
</IfModule>