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
Related
This one is a bit tricky for me, I want to make this URL as minimal as possible. Ideally I would like to change this URL:
http://www.example.co.uk/profile/profile.asp?profile_id=1&top=1&abt=2&ft=3&school=Something%20School
to:
http://www.example.co.uk/something-school/
Using .htaccess file.
This means we would be using the school get variable to replace the .asp file name, getting rid of /profile/ as well as the other get variables.
Is this possible? If so how? If not could you potentially give me an alternative?
Any help would be greatly appreciated!
EDIT
A user Badhorsie has wrote a rewrite rule for me which does the exact conversion. Unfortunately the webpage fails to load as these get variables are unfortunately necessary for the page to load.
I am guessing it is not possible to hide the get variables. In which case would it be possible to retain the get variables but to keep them clean? Looks like the directory is also necessary?
Perhaps something like: www.example.co.uk/profile/something-school/1/1/2/3
We can get rid of the school get variable as it is not needed (only needed to replace the profile/profile.asp section.
Try this:
RewriteCond %{QUERY_STRING} (.+(?=&school))school=([\w%-]+)
RewriteRule ^profile/profile.asp /%2?%1 [L,NE,R=301]
Did you tried this RewriteRule. Try adding this code in your .htaccess file.
RewriteRule ^http://www.example.co.uk/profile/profile.asp?profile_id=1&top=1&abt=2&ft=3&school=Something%20School?$ http://www.example.co.uk/something-school/ [L]
If someone tries to access this page:
www.myhomepage.com/m/40921
I want the URL (above) to remain the same but the content should be from this page:
www.myhomepage.com/msg?t=40921
If it must be exactly like you said, then use this
RewriteRule ^m/([0-9]+)\/?$ msg?t=$1
But I think you made a typing error. Shouldn't it be msg.php? If so, use this:
RewriteRule ^m/([0-9]+)\/?$ msg.php?t=$1
And in your php file, you can get the t with $_GET['t'].
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
I am having a bit of a problem with .htaccess and numbers.
I have a directory
http://somedomain.com/directory/
When I add a number to it, I want to use htaccess to create a variable.
http://somedomain.com/directory/
RewriteRule ^[0-9]+$ index.php?action=viewclient&clientnumber=$1 [L]
When I put this URL, I want the clientnumber to be created
http://somedomain.com/directory/5
However, when I put no number in it, it still creates the $action variable with a blank $clientnumber.
http://somedomain.com/directory/
Is there a change I need to make to the htaccess to only add these variables if there is a number in the URL but blank. To skip this htaccess line if no variable is added.
Thankyou
EDIT: i cant get the URL's to work. from the answers provided. i'm off to bed & will re-check this over the weekend.
if I put /directory/ I get what i want. if i put /directory/4 or directory/87 (any number) i get a 404 error.
EDIT2 - WEIRD ???
I'm off to bed, but decided to try again & selected a different client. the URLs are the SAME format.
some clients work (display the correct page), other URL's (CORRECT URL), show a 404 page.
I'll try & debug more in the weekend. - very weird. (I'll add a debug script / log at the top of my index.php - to see if the page is even loading.)
EDIT 3:- I've added a small log script at the top of index.php - it DOES record the valid / working URLs, but does not activate if its a 404 error (the error is the SAME format URL - so should activate index.php - & then my index.php should do the calcualtions / checks to see if the values are valid. but its not even getting to my php script.
OK, i'm off to bed - will retry in the weekend.
RewriteRule ^[0-9]+$ index.php?action=viewclient&clientnumber=$1 [L]
$1 refers to the first capture group. You don't have any capture groups in your regular expression. This is correct:
RewriteRule ^([0-9]+)$ index.php?action=viewclient&clientnumber=$1 [L]
Changing it to
RewriteRule ^([0-9])$ index.php?action=viewclient&clientnumber=$1 [L]
(no + after [0-9] )
will mean the rule is only executed if the number 0-9 is present in the request
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?