htaccess redirect to dynamic subpages - php

I am trying to use a redirect to the same page but with a query string.
RewriteRule ^fixtures(.*)$ views/fixtures.php
RewriteRule ^fixtures/(.*).([a-zA-Z_-]*) views/fixtures.php?date=$1
When I click any links from the page, nothing happens. In chrome debugger it cancels the request.
Any idea what I am doing wrong?
Cheers

It would be great to see a working example but just looking at your rewrite rules it seems there might be a small issue with your regex.
The issue I can see is that your first line expects something like:
example.com/fixtures*absolutelyanythingeleseontheurl*
But I think this is being overidden by the next line which is capturing two sections of the url, anything after fixtures/ and anything after the fullpoint that is a-zA-Z, _ or -. But your initial .* has already taken care of that.
RewriteRule ^fixtures/(.*).([a-zA-Z_-]*) views/fixtures.php?date=$1
Have you tried something like this:
RewriteRule ^fixtures$ views/fixtures.php
RewriteRule ^fixtures/(.*) views/fixtures.php?date=$1
The above would do the following:
example.com/fixtures/ will resolve to views/fixtures.php
and the next line would resolve to the following:
e.g. example.com/fixtures/22-06-2014 to views/fixtures.php?date=22-06-2014

Related

Problems with mod_rewrite dynamic URL

I've made my own blog on my domain and for displaying the articles I'm using a dynamic URL to display the article content on the page.
An example how it looks now:
http://www.blog.madetocreate.nl/artikel.php?id=47
Now I've searched for a mod_rewrite method for days, but everything I tried fails. The service provider told me mod_rewrite is enabled and I only have to access it with a .htaccess file.
The file I have at this moment contains the following code:
RewriteEngine on
RewriteRule ^artikel/([^/]+)/?$ artikel.php?id=$1 [L]
I want to get the url to look like this:
http://www.blog.madetocreate.nl/artikel/47/title-of-the-post-here/
But so far I can't even get managed to get the first "artikel/47/"
Any idea on what I'm doing wrong?
Thanks!
The regex should look like the following:
^artikel/([0-9]+)/([\w-]*)$
Which gives you:
RewriteEngine on
RewriteRule ^artikel/([0-9]+)/([\w-]*)$ artikel.php?id=$1 [L]
([0-9]+) matches 1 or more number
([\w-]*) matches 0 or more word character and -, meaning that you can have both artikel/47/ and artikel/47/title-of-the-post-here

php htaccess url rewrite issue

Im trying to get a url rewrite to work and Im not getting any where. basically I want to have three pages one for music and then general products and the other for mvies, each will have a query string of &pagetype=general etc but the url would be http://shopnbuy.vacau.com/browse/computers.html?pageType=general which will redirect to general_products.php and then there would music_products and also movie_products. So they all have the same url except there is a different parameter &pageType=. Below is my htaccess file this is my first attempt at doing this
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
RewriteEngine On
RewriteRule ^browse/([a-zA-Z0-9_-])\.html general_products.php?department=$1&pageType=general
RewriteRule ^browse/([a-zA-Z0-9_-])\.html music_products.php?department=$1&pageType=music
RewriteRule ^browse/([a-zA-Z0-9_-])\.html movie_products.php?department=$1&pageType=movie
url suppose to look like this
http://shopnbuy.vacau.com/browse/computers.html
All Im getting is an error for each url rewrite
I don't know much htacess, but I used it lately and I found out that you can't actually use "/" effectively. I suggest you change it for "_".
As far as I understand, it may be due the "/" works as the one indicating each argument (I mean A / B sorta logic)
Not sure if I explain myself correctly, hoping someone knowledgeable corrects me since I'm eager to learn about it as well.
RewriteRule ^browse_computers / general_products.php?department=$1&pageType=general ([a-zA-Z0-9_-])
RewriteRule ^browse_music / music_products.php?department=$1&pageType=music ([a-zA-Z0-9_-])
RewriteRule ^browse_movies / movie_products.php?department=$1&pageType=movie ([a-zA-Z0-9_-])
The result, however, will be this:
http://shopnbuy.vacau.com/browse_computers
Bit unsure of your initial url but try this
^browse_(.+)/(.+)$ browse/computers.html?pageType=$1

mod_rewrite url

I noticed on youtube their url does not have a file extension and querystring. I've been trying to emulate something similar but found I had to either include the file extension or a trailing slash.
members.php?agefrom=20&ageto=40&city=london (works)
members/?agefrom=20&ageto=40&city=london (works)
members?agefrom=20&ageto=40&city=london (doesnt work)
So I was just wondering how can I get the third case to work? i've tried a few things in the htaccess file.
RewriteRule ^members$ index.php?page=members&%{QUERY_STRING} [QSA,L]
I have tried the above but to no avail.
Any help would be appreciated.
The RewriteRule that you posted is correct for members.php? and for members? It should not work with members/
You must have additional RewriteRules before this one that are getting applied first and are affecting this rule.
However, here is a rule that should still work for you:
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
The /? is saying to match if the slash exists or if it doesn't exist.
Have you tried to remove the $ on the end?
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
This did work in the end, all I had to do was move it nearer the top of the htaccess file. I had the following line which I guess was being read instead.
....
RewriteCond %{REQUEST_URI} ^/members$ [OR]
....
I am changing my approach to SEO URL's because I was trying to find articles on how the googlebot actually crawls forms and how it prefers the GET method. I was using jquery to alter my action parameter to write the following URL:
/members/london/18-to-25
I dont know how much google likes jquery and whether it would scan javascript code. I am assuimg it just follows the HTML code and having done some research I have changed my form to use the GET method and so the bot can crawl my form without complaining so now my URL looks like this:
/members?location=london&agefrom=18&ageto=40
I am on the right track to assume this? or should I just stick with jquery to rewrite the action parameter for an seo friendly URL?

.htaccess rewrite, for all directories

So I have errors which are passed by the url, for example
index.php?error=nojs
will then be parsed by PHP to return an error message, for example: Please enable Javascript
I'm using the following line in my .htaccess to make the url easier to manage
RewriteRule ^ERROR_(.*)$ index.php/?error=$1&%{QUERY_STRING} [L]
It makes my URL look like this:
site.com/ERROR_nojs
The problem is, this only works for the root,
index.php?error=nojs works fine however
test/index.php?error=nojs does not?
So how can I convert the variable for every directory?
Thank you. (My original script handles hundreds of errors and filters out ones that might be useful to output to the user. It would be stupid to redirect them to the index just so they can see a small with an error message in it)
EDIT:
as Shai Mishali pointed out removing the '^' before ERROR did the trick.
RewriteRule ERROR_(.*)$ index.php/?error=$1&%{QUERY_STRING} [L]
But I forgot to tell you I have another variable ?page=
I need get that vairbale and add it to the url in order for this to work..
e.g:
index.php?page=home&error=nojs
= site.com/home/ERROR_nojs
so
www.site.com/?page=home&error=nojs = www.site.com/home/ERROR_nojs
or
www.site/?page=about&error=unknown = www.site.com/about/ERROR_unknown
I'm pretty sure your problem is your rule is looking for something that starts with ERROR (the ^ sign) .
/ERROR starts with error, which works in your root , but
/tests/ERROR starts with tests , so it won't recognize it.
Try removing the ^ sign and see what happens.
Shai.
You can use below code
RewriteRule ^([a-z0-9A-Z]+)/([a-z0-9A-Z]+)$ ./index.php?page=$1&error=ERROR_$2 [NC]
Your URL site.com/home/ERROR_nojs will treated as index.php?page=home&error=nojs and you can get the values by GET method. For more info

.htaccess RewriteRule for Flat Links

I am pretty new to using the RewriteRule, so I am likely missing something obvious, but I have a PHP script that takes URL variables like this:
{baseurl}properties.php?prop=Property-Name
I would like to create RewriteRules so that anyone who types in this script name/variable combo would have their URL rewritten to:
{baseurl}/properties/Property-Name
As well as ensuring that anyone who types in the flat-link url, actually calls the script with the right variable name and value.
I have been referring to this link and I have found related threads:
Mod_rewrite flat links
Mod_rewrite trouble: Want to direct from ?= to a flat link, nothing seems to work
But, I am obviously doing something wrong, as I cannot get this URL to work the way I want. I am currently using the following code, which appears to do nothing (aside from rewriting the URL to include the www, and redirect requests for index.php to the site root):
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^baseurl.com$ [NC]
RewriteRule ^(.*)$ http://www.baseurl.com/$1 [R=301,L]
RewriteRule ^index.php / [R=301,L]
RewriteRule ^properties/([0-9A-Za-z]+)/$ /properties.php?prop=$1
The issue is clearly with the last RewriteRule, assuming nothing above is affecting it. Again, I am likely doing something ridiculous. Can someone please explain what I am doing wrong?
Thanks for your help.
At a quick glance, it appears that you forgot to include the dash in your regular expression and you included trailing slash. Use this instead:
RewriteRule ^properties/([0-9A-Za-z-]+)$ /properties.php?prop=$1
If you look at your rule ^properties/([0-9A-Za-z]+)/$ you see that it needs to end with a forward slash. You can either remove that or make it optional like ^properties/([0-9A-Za-z]+)/?$.

Categories