im trying to beautify my urls, my urls are like that
http://www.mysite.com/details.php?id=19&object=1
object=1 (videos) or object=0 (articles)
i want change this url to
http://www.mysite.com/videos/19
of course i make videos because i mentioned that when object =1 means videos
and when object =0
i want this
http://www.mysite.com/articles/19
I tried using this from some tutorials , but didnt work.nothing happen.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^videos/([a-zA-Z]+)/([0-9]+)/$ details.php?object=$1&id=$2
and also how do i do the if condition with RewriteCond to check if object is 1 or 0 , if 1 then print videos else articles.
any help would be much apreciated.
It is better to use RewriteMap for your case here. Here is a sample how to use it:
Add following line to your httpd.conf file:
RewriteMap objMap txt://path/to/objectMap.txt
Create a text file as /path/to/objectMap.txt like this:
articles 0
videos 1
Add these line in your .htaccess file under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^([^/]+)/([0-9]+)/?$ /details.php?object=${objMap:$1}&id=$2 [L,QSA]
Advantage: With this setup in place, you can edit or recreate the file /path/to/objectMap.txt anytime you have a new object id mapping without any need to add new rules.
UPDATE: If you have no control over Apache config you will have to deal with multiple rewrite rules (one each of each object id mapping) like this:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+details\.php\?id=([^&]+)&object=1\s [NC]
RewriteRule ^ /videos/%1? [R=302,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+details\.php\?id=([^&]+)&object=0\s [NC]
RewriteRule ^ /articles/%1? [R=302,L]
RewriteRule ^videos/([0-9]+)/?$ /details.php?object=1&id=$1 [L,QSA,NC]
RewriteRule ^articles/([0-9]+)/?$ /details.php?object=0&id=$1 [L,QSA,NC]
Related
I have a URL like this :
https://example.com/coins/1&order_by=today and several categories such as:
https://example.com/coins/1&order_by=new
https://example.com/coins/1&order_by=trending
You can also change the page so it will be 2 instead of 1 etc.. like : https://example.com/coins/2&order_by=today
I would like to change these to have https://example.com/today?page=1 etc instead of this above
I have read many responses on this subject and I searched the web, but unfortunately no solution works.
I tried this but it does not works:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*&)?order_by=new(&.*)?$
RewriteRule ^(.*)$ $1?new/%1/%2 [L]
Try following htaccess Rules, please make sure to place these Rules at top of your htaccess file. Make sure to place your htaccess file inside root folder along with coins folder(not inside coins folder).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{THE_REQUEST} \s/coins/(\d+)&order_by=(\S+)\s [NC]
RewriteRule ^ /%2?page=%1 [R=301,L]
i am trying to have my urls look like cleaner using the below but its not working i don't know if i should add anything else,
this is how it looks now
example.com/product.php?product_id=144&product_name=50ml-bottle-with-glasses,-shirt
this is how i want them to look
example.com/product/144/50ml-bottle-with-glasses,-shirt
basically here's what i used
RewriteEngine On
RewriteRule ^product/([0-9]+)/([A-Za-z0-9-]+)/?$ product.php?product_id=$1&product_name=$2 [NC,L] # Handle product requests
Thank you
This should work :
RewriteEngine On
#1)redirect from "/product\.php\?product_id=123&product_name=foo" to "/product/123/foo"
RewriteCond %{THE_REQUEST} /product\.php\?product_id=([^&]+)&product_name=([^\s&]+) [NC]
RewriteRule ^ /product/%1/%2? [NE,L,R]
#2)internally map "/product/123/foo" to "/product\.php\?product_id=123&product_name=foo"
RewriteRule ^product/([0-9]+)/(.+)/?$ product.php?product_id=$1&product_name=$2 [B,NC,L]
There are comma into your parameter product_name. Try to change your .htaccess and add comma into your regular expression for this parameter: ([A-Za-z0-9-,]+). So your htaccess will be something like this:
RewriteEngine On
RewriteRule ^product/([0-9]+)/([A-Za-z0-9-,]+)$ product.php?product_id=$1&product_name=$2 [NC,L] # Handle product requests
I'm working on redirecting a bunch of URLs to a new schema, basically from:
http://www.gamikaze.tv/?XHh-1Z56av0
to:
http://www.gamikaze.tv/#!/videos:XHh-1Z56av0
I tried to find examples of that, but all queries examples I've seen are using a key/value pair, and this only contains one value. Also, the URL doesn't fundamentaly changed - both URLs are using index.php. How could that be achieved using an .htaccess?
Try this code.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^([a-z0-9-]+)$ [NC]
RewriteRule ^$ /#!/videos:%1? [L,R=301,NE]
Bellow is my basic .htaccess code
Options +FollowSymLinks
RewriteEngine on
RewriteBase /artist/
RewriteRule ^member/([^/]*)$ /artist/index.php?member=$1
Bellow is my static .htaccess code
Options +FollowSymLinks
RewriteEngine on
RewriteBase /artist/
#we module doing some action
RewriteRule ^member/([^/]*)/([^/]*)/action/([^/]*)/$ /artist/index.php?member=$1&module=$2&action=$3
#action operation e.g create
RewriteRule ^member/([^/]*)/([^/]*)/action/([^/]*)/opt/([^/]*)/$ /artist/index.php?member=$1&module=$2&action=$3&opt=$4
#if only module calls
RewriteRule ^member/([^/]*)/([^/]*)/$ /artist/index.php?member=$1&module=$2
RewriteRule ^member/([^/]*)/([^/]*)/opt/([^/]*)/$ /artist/index.php?member=$1&module=$2&opt=$3
RewriteRule ^member/([^/]*)$ /artist/index.php?member=$1
RewriteRule ^member/0/$ /artist/index.php?member=$1
RewriteRule ^member/([^/]*)/([^/]*)/opt/([^/]*)/$ /artist/index.php?member=$1&module=$2&opt=$3
RewriteRule ^member/([^/]*)/([^/]*)/opt/([^/]*)/topicId/([^/]*)/$ /artist/index.php?member=$1&module=$2&opt=$3&topicId=$4
can any one help me to write dynamic RewriteRule to add query sting in my RewiteRule so that i dont have to add manually RewriteRule and also it will reduce the line of my .htaccess code
some thing like:
RewriteRule ^member/([^/]*)[dynamic code]$ /artist/index.php?member=$1[dynamic code here]
At that point you rewrite all requests to /artist/index.php without constructing a query string, and index.php examines the request URI to populate its own variables.
i.e. have only one rule
RewriteRule ^member/.* /artist/index.php
index.php can look at $_SERVER['REQUEST_URI'], explode on slashes, etc.
mod_rewrite's job is not to implement complex logic. You put it in your application.
http://en.wikipedia.org/wiki/Front_Controller_pattern
sorry for this simple question, however i still cant get my head round using .htaccess
I'm trying to convert:
search.php?s=dvd&page=1
to
/Search/dvd/page1.html
Thanks,
Jack
I think something like:
RewriteRule ^search/([A-Za-z]+)/page([0-9]+)\.html$ search.php?$1&page$2
Should do the trick.
Further reading here: http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php
you must put your link "/Search/dvd/page1.html" in the page and with htaccess it will convert to the "search.php?s=dvd&page=1" . i hope it be usefull :)
sample correct htaccess code :
RewriteEngine On
RewriteRule Search/(\d+)/page?(\d+)\.html$ search.php?s=$1&page=$2 [NC,L]
If I understand the question OP wants to convert php to html redirection. Try this rule in your .htaccess file:
RewriteEngine on
Options +FollowSymlinks -MultiViews
# to match /search.php?s=dvd&page=1
RewriteCond %{QUERY_STRING} ^s=([^&]*)&page=(.*)$ [NC]
RewriteRule ^search\.php/?$ /Search/%1/page%2.html? [R,L,NC]
# to match /search.php?page=12&s=dvd
RewriteCond %{QUERY_STRING} ^page=([^&]*)&s=(.*)$ [NC]
RewriteRule ^search\.php/?$ /Search/%2/page%1.html? [R,L,NC]
R=301 will redirect with https status 301
L will make last rule
NC is for no case comparison
%1 and %2 are the query parameter values
I think you want make clean URI, so if user type url like this:
search/televisi/page4.html
it same as like search.php?s=dvd&page=1
Add this code:
RewriteEngine On
RewriteRule ^search/([a-z]+)/page([1-9]+).html$ search.php?s=$1&page=$2