htaccess url'redirects and rewrites - .htaccess

Hope someone can help me out 'cause I utterly suck at .htaccess tweaking.
I'm developing a php based site. On this site I include specific pages based on a query string
eg: index.php?q=somePage.
In the .htaccess I look for eg: /somepage/ and tell the server to load q=somePage. When this is done the server strips all other query strings from the url ergo it does not pass them.
Now to the question, is there someway to setup the .htaccess to catch the [q] parameter and pass along all other querystrings (server side)?
eg: if I call /somepage&parameter=someparameter or /somepage/&parameter=someparameter the server will rewrite the url to /somepage/ but call the page (serverside) like so:
index.php?q=somePage&parameter=someparameter
Here's how my htaccess looks now:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?q=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?q=$1
if not I guess I'll just have to store the parameters in a session and pass 'em that way. But I hope someone can help :)
PS: If you know of a book that deals with the htaccess and can bring me from complete n00b to expert, it would be epic if you could point me in the right direction. the same actually goes for regEx ;)

Usual lot of framework move the logic of parsing the request inside the PHP
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
If the URL not identify a file or e dir, pass it to index.php
EDIT
You can see http://www.zytrax.com/tech/web/regex.htm for an introduction to regexp, but the suggest htaccess pass ALL the parameters to index PHP.
Use phpinfo(); to see how your reqest is seen by PHP
As your request here the index.php
<?php
phpinfo();
here some query you can try (I suppose your code on root of localhost):
http://localhost/
http://localhost/test
http://localhost/a/b
http://localhost/?p1=v1&p2&v2&p3&p4
http://localhost/a/b?p1=v1&p2&v2&p3&p4
http://localhost/a/b/?p1=v1&p2&v2&p3&p4

Related

htaccess URL rewrite pattern format

I've had a good look through all the other htaccess url rewrite questions, but all of them deal with the reverse of my problem.
The site I am working on takes content from child pages in WordPress and presents them as anchored sections on the parent page. The problem for the site now is that if Google (or the built in search, though that can probably be done in the templates) links to one of the child pages it will direct users to the single page rather than the correct section of the parent page.
I was hoping to come up with a URL rewrite pattern which would reformat the URL to what I need, but it doesn't seem to work... and I know that's because I have done it wrong!
The URL which needs to be rewritten is something like:
website.com/parent-page/child-page/
I need this to be rewritten to:
website.com/parent-page/?subpage=child-page
My initial stab at it looks like this, but I know I've misunderstood something about how to format the pattern.
RewriteRule ^/$1?subpage=$2 ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$
Can anyone help format this correctly or point out where I'm going wrong?
You can use:
RewriteEngine on
# If the request is not for a valid file/directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/?subpage=$2 [QSA,L]
No initial / in htaccess RewriteRule first uri
And optional final /
RewriteRule ^([^/]+)/([^/]+)/$ /$1/?subpage=$2 [L]
The htaccess rewrites didn't seem to work, so I've created a PHP redirect instead by taking the page slug, page parent permalink and mashing the two together.
Not the most elegant solution, I expect, but it works.

How to redirect to a URL with a query string?

I've seen questions regarding redirecting a URL with a query string to a new URL that doesn't contain a query string.
Unfortunately I have to do this in reverse, my knowledge of redirecting in Apache isn't good, and I can't find any information about doing this.
So for example I need to redirect something like:
/news/news-item
to:
/news?item=news-item
The new URL structure is obviously not ideal, but this is something that is out of my control.
I've tried:
RedirectMatch 301 /news/news-item http://www.example.com/news?item=news-item
But obviously this doesn't work.
From what I understand I need to use RewriteRule, can somebody point me in the right direction?
I'm probably to late with my answer, but this is how I would have done it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news/(.*) /news?item=$1 [R=301,L]
</IfModule>
I would also use RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d. In case a file or a directory with the specified name exists, it would not procede with the rewrite rule below.
I managed to figure this out after a bit of research.
I'm not sure if this is 'ideal' way of doing this, but it seems to work well from my perspective.
Instead of using a Redirect of a RedirectMatch it's possible to achieve this using RewriteRule, something like:
RewriteRule ^news/?(.*) http://example.com/?posts=list [R=301,L]
This will redirect any request to news or news/x (where x could be anything) to:
http://example.com/?posts=list
I should have made it clearer in my initial question that the query string didn't need to be generated using part of the old URL, if you need to do this I think you'll need to look into something like this:
How to redirect URLs based on query string?

How to redirect using .htaccess using Apache?

Please help me to redirect using .htaccess like below.
http://info.domainname.com/blog/?Tag=somedynamictag
to
http://domainname.com/tag/somedynamictag
Thanks!
First of all I don't understand the purpose of rewriting http://info.domainname.com/blog/?Tag=somedynamictag to http://domainname.com/tag/somedynamictag when it had to be the other way round. People rewrite URLs to clean them (ie. remove characters like ?,&,=, etc.) but you are adding all these and making the URLs cumbersome.
I think you didn't properly understand the concept of URL rewriting. Let me explain a little.
When any URL is accessed on your website, the URL that the USER types or clicks (in your case http://domainname.com/tag/somedynamictag) is rewritten. But your question tells me that you think the other way. Your understanding is that the visitor clicks http://info.domainname.com/blog/?Tag=somedynamictag and will be rewritten to http://domainname.com/tag/somedynamictag. THIS IS WRONG!!. If you set up your website this way, each and every URL at http://domain.com/ must exist as a separate file or directory which a dynamic website like you seem to be developing is not expected to have. So I assume you have understood that you have either misformed the question or you have you have misunderstood the concept of URL rewriting. Following is the .htaccess code to redirect http://domainname.com/tag/somedynamictag to http://info.domainname.com/blog/?Tag=somedynamictag.
RewriteEngine On
RewriteCond %{HTTP_HOST} domainname.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9]+)\/(.*)$ http://info.domainname.com/blog/?$1=$2 [L,R=301]
If you think you have correctly typed the question and understood url-rewriting right and are sure what you are trying to do is right, then here's the htaccess code (if you want to redirect http://info.domainname.com/blog/?Tag=somedynamictag to http://domainname.com/tag/somedynamictag).
RewriteEngine On
RewriteCond %{HTTP_HOST} info.domainname.com
RewriteRule ^blog\/\?([A-Za-z0-9]+)=(.*)$ http://domainname.com/$1/$2 [L,R=301]
If that doesn't work, in index.php at http://info.domainname.com/blog/, place the following code:
<?php
header("HTTP/1.1 301 moved permanently");
header("Location:http://domainname.com/blog/tag/".$_GET['Tag']);
?>
And thank you for asking this question. While answering it, I learned many things.
Hope that answers your question,
Peace...

Change Displayed URL Structure using mod_rewrite NOT Working

I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]

Creating a redirect in htaccess for incoming requests

how can i redirect all requests coming on
foo.bar.com/some-thing
to be redirected to
foo.bar.com/some-thing-else
also can you please hook me up to some good learning resources for learning htaccess, i am very new to this development stuff.
Regards.
Edit
following is the code i have been trying with (without success)
RewriteRule ^some-thing?$ index.php/some-thing-else [QSA,L]
try something like this :
in the .htaccess put this :
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule ^(.*)-thing?$ index.php?route=$1-thing-else [QSA,L]
in the index php put this for testing :
<?php echo var_dump( $_GET ); ?>
browse to your site ie http://localhost/some-thing or http://localhost/dirty-thing.
hope you can modify this to your exact needs.
It is mod_rewrite Apache module what you should be working with to do such redirects. It's not simple to learn, still very powerful tool. There are multiple tutorials learning it. Try http://www.easymodrewrite.com/ for beginning.

Resources