mode rewrite function in .htaccess file - php

Hom many times can I use mode_rewrite function and rule in .htaccess file?
like:
RewriteEngine On
RewriteBase /public_html
RewriteRule ^([a-zA-Z0-9\-]+)$ /game.php?game_name=$1

You can use as many as you like, as long as you add them on newlines. Make sure your rules are not getting into a conflict though.
Some information about parameters you can use:
http://httpd.apache.org/docs/current/rewrite/flags.html
You can test your .htaccess with this website:
http://htaccess.madewithlove.be/

Related

How to implement Apache Rewrite Rule correctly?

Following is my rewrite rule code written in the .htaccess file placed in the
beta1
folder.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /beta1/
RewriteRule /(.*) read.php?post=$1 [R,NC]
Currently the URL is:
http://localhost/beta1/read.php?post=Happy+Mother+Day.
And i want the link to appear like:
http://localhost/beta1/Happy-Mother-Day.
Do I also need to change the way "read.php" extracts data from the database? If yes, then how?
Do i also need to make any changes in the "read.php" file?
The rewrite code above is not working.
hey please try this rule
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^([A-Za-z0-9-_]+)?$ read.php?post=$1 [NC,L]
And read.php file
echo str_replace('-',' ', $_GET['post']);

How i know .htaccess file is working or not?

I use this htaccess url
mywebsite.com/xyz/search.html
here xyz is a folder in root
in .htaccess is use the code for this url
# enable apache modRewrite module #
RewriteEngine On
RewriteBase /
RewriteRule ^([^//]+)/?(^/*)?.ht(m?ml?)$ index.php?page=$1 [L,QSA]
now i want this
xyz/search.html is hit the
url xyz/index.php?page=search
but this:
RewriteRule ^([^//]+)/?(^/*)?.ht(m?ml?)$ index.php?page=$1 [L,QSA])
code is not working..
any idea regarding this...
Type some random characters in your .htaccess file and try to reload your page, if you see error 500 then your .htaccess file is working, put your random characters after "RewriteEngine On" .
Not sure that rule would work. Does this one do the job?
RewriteRule ^.*/(.*)\.html?$ index.php?page=$1 [L,QSA]
Also, if you want to test the rule with a bit more visibility, you can add R=302 to the flags, that way your browser will get a redirect and you'll be able to see the rewritten URL in the address bar
I wonder why people don't use RewriteLog.
Put in the same place:
RewriteLog /tmp/rewrite.log
RewriteLogLevel 3
It slows down the server but for debugging it's made for.
Try:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*?)/?([^/]+)\.ht(m|ml)?$ $1/index.php?page=$2 [L,QSA]
There is a very handy online tool for testing htaccess files. Simply paste your redicrect rules in the form provided and test various urls to see if they are redirected or left untouched.... soooper easy!

Why doesn't this mod_rewrite rule work?

When I try to use this rule:
RewriteEngine On
RewriteRule ^articleID/([^/]*)$ /viewArticle.php?articleID=$1 [L]
It sends me to a 404 page.
I am not well-versed in how to use an htaccess, so I most likely did something wrong. I generated it with a tool at generateit.net
I'm trying to access http://majornoob.com/devel/testdesign/articleID/5 which would equal http://majornoob.com/devel/testdesign/viewArticle.php?articleID=5
Try to add a RewriteBase:
RewriteBase /
Also make sure that your server accept htaccess files. Does the Server configure contains a AllowOverride all statment in your Directory Directive.
With the path, it should work:
RewriteRule articleID/([^/]*)$ /devel/testdesign/viewArticle.php?articleID=$1 [L]

URL Rewriting with .HTACCESS not working in php

My URL looks like this:
http://127.0.0.1/website/comments.php?topic_id=16
I want to make it look like this:
http://127.0.0.1/website/comments/my-news-article/16.php
I have applied this
RewriteEngine on
RewriteRule ^comments/([a-zA-Z0-9_-]+)/([0-9]+)\.php$ comments.php?id=$2
I have also turned on Rewrite_module in Apache.
But it's not working.
Always consider the entire URL path. Yours includes /website, so:
RewriteEngine on
RewriteBase /website
RewriteRule ^comments/([a-zA-Z0-9_-]+)/([0-9]+)\.php$ comments.php?id=$2
RewriteBase is used for per-directory directives. Try
RewriteEngine on
RewriteRule ^/website/comments/([a-zA-Z0-9_-]+)/([0-9]+)\.php$ /website/comments.php?topic_id=$2
Works for me. Note that I also changed the query string parameter at the end of the line to "topic_id". Maybe this is another cause of error. If it still fails, check that the configuration is in the right virtual host config.

rewrite .php extension

Please how can I rewrite
Could anybody please rewrite this url?
http://localhost/display_news_cat.php?news_cat_id=14&p=2
to
http://localhost/display_news_cat/14/2
Thank you
You can do thtat with a .htaccess file
put this in a .htaccess file and place it in de root of your site
apache must have mod-rewrite on
RewriteEngine on
RewriteRule ^news.php news [QSA,L]
try this for all files
RewriteEngine on
RewriteRule ^(a-zA-Z0-9).php $1 [QSA,L]
Assuming you have installed the rewrite module, you could put this in your virtualhost:
RewriteEngine ON
RewriteRule /newsdev/news /newsdev/news.php [L]
Check this site for more information on RewriteRule:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
But I doubt this is the best way. The rewritemodule is usually not the most efficient way to do easy things like these. Depends on what other complex things you want to rewrite.
With mod_rewrite
http://www.easymodrewrite.com/example-extensions
You need to work with the .htaccess file in your site root... you'll have to create one if it's not there.

Categories