How to write .htaccess dynamicaly (i.e from query string) - .htaccess

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

Related

how to replace "?" and "=" sign with "/" in URL - PHP htaccess

i am working on project, which is running XAMPP localhost and PHP MYSQLI,
my question : how i replace "?","=" signs with "/" slash. ?
like, my url is "archive?date=2017-06-02&p=4"
and i want to force it "archive/2017-08-02/4"
i found many codes on stackoverflow and some other sites, but that are not working for me.
if codes are working then, CSS files and GET method doesn't work on my project.
complete code of .htaccess is given below.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^=]*)=([^=]*)=(.*) /$1/$2/$3 [N]
RewriteRule ^([^=]*)=([^=]*)$ $1/$2 [L]
RewriteRule ^home index.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^about about.php [NC,L]
RewriteRule ^article article.php [NC,L]
RewriteRule ^news news.php [NC,L]
RewriteRule ^video videos.php [NC,L]
RewriteRule ^video?vid=([0-9]+) videos.php?q=$1 [NC,L]
RewriteRule ^article?num=([0-9]+) article.php?num=$1 [NC,L]
RewriteRule ^editorial?num=([0-9]+) editorial.php?num=$1 [NC,L]
RewriteRule ^news?news=([0-9]+) news.php?news=$1 [NC,L]
You cannot check against the query string in a rewrite rule. You need rewrite conditions for that:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2?
Demo here: http://htaccess.mwl.be?share=81e85c09-d505-5206-ab14-6c5059107808
If you want to actually redirect just add [R=301,L] to the end of the RewriteRule.
However, looking at the above I suspect you have your script sitting listening at /archive/index.php?data=foo&p=bar but want URLs to be like /archive/date/p, ie pretty.
This is actually a very common misconception about how htaccess URL rewrites work when you first get into them.
RewriteRules will mask or redirect URLs for you but they cannot change the underlying location a script is located at and thus the address used to pass it information.
In other words - you can mask /archive/index.php?data=foo&p=bar as /archive/date/p so that requests made to /archive/date/p resolve to /archive/index.php?data=foo&p=bar, but you cannot make it so that if you enter /archive/index.php?data=foo&p=bar as URL you have the URL change to /archive/date/p while still serving content from /archive/date/p. It has to be either or.
If this all sounds about right my advice would be as follows:
First, put your code into a different file, say /archive/script.php.
Next add the following to your htaccess:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2? [R=301,L]
RewriteRule ^archive/([^/]+)/([^/]+) /archive/script.php?date=$1&p=$2
Note that the first two lines are the same as before, but now there is a new line that looks for the masked URL format of /archive/date/p and sends it off to the actual script, which is handled by the new RewriteRule.
The behaviour of the new rule is demoed here: http://htaccess.mwl.be?share=06751667-f16f-5c13-91eb-dd5cffdc6db3
Hope this makes sense / helps.

URL rewrite for a clean format

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

Laravel clean URLs

Working on a Laravel project and trying to get a simple URL rewrite working. Here is the .htaccess file, located in the '/public' folder — you can see I have added one rule
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# above is default, rule added by me:
RewriteRule ^articles/page/([a-zA-Z0-9-/]+)$ /articles?page=$1 [L]
</IfModule>
However, this just 404s. I suspect the default rule is breaking it somehow but don't know enough to fix it.
Can anyone help, please?
It would appear to me that your rule to convert articles/page/{slug} into articles?page={slug} is never hit. Because the rule is the bottom-most and your index.php rewrite rule says it's the "last" rule ([L]) and matches anything it wins every time (i.e. there's never any fall-through to your new rule).
Move your articles-specific rule between RewriteBase and RewriteCond and don't mark it as the last rule (get rid of [L]) and try again. That should transform articles/page/{slug} into articles?page=slug and then pass that on to the index.php rule.
You may also need the QSA rewrite rule option to make sure the query string you create works with any query string that exists already.
See:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
http://httpd.apache.org/docs/current/rewrite/flags.html
Try changing order of your rules:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
# above is default, rule added by me:
RewriteRule ^articles/page/([a-zA-Z0-9/-]+)/?$ /articles?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Or you can use Laravel's routes:
Route::get("/articles/page/{article}", "ArticleController#showArticle")
->where("article", "[a-zA-Z0-9/-]+");
And the controller:
class ArticleController extends Controller {
public function showArticle( $articleID ) {}
}
I think this is a better approach.

rewrite rule in htaccess didnt work

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]

URLRewrite rule for a URL that only contains one value after the question mark

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]

Resources