htaccess URL rewrite pattern format - .htaccess

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.

Related

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?

.htaccess file for pretty urls

(This seems like it should be one of the mostly commonly and easiest addressed questions on the web, since most websites have "pretty" or "clean" urls. But in all my searches, it's proven to be one of the most complex.)
In the simplest form, I would like be able to enter example.com/about into the url bar and have the server return the file example.com/about.php. As it is, I have to enter or link to example.com/about.php, which is not SEO or user friendly. This isn't about complex strings--the file could just as easily be example.com/about.html.
I have some code I'm attempting to use with an .htaccess file, but it seems to do nothing:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-z]+)/$ /$1.php
I know that the .htaccess file is working, because the 404 redirect I have set up (which appears in the .htacces doc below the code I've included here) is functioning properly, especially when I'm trying access example.com/about and I get my 404 page.
Thanks for your help!
Your last rule is designed to match http://example.com/about/ . I think what you want is
RewriteRule ^([a-z]+)$ $1.php

htaccess url'redirects and rewrites

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

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...

mod_rewrite question

Hi I have a basic .htaccess file in a subdirectory folder called 'support' which looks like this:
RewriteEngine on
RewriteRule ^knowledgebase/([^/]*) knowledgebase.php?article=$1 [NC,L]
RewriteRule ^knowledgebase/category/([^/]*) knowledgebase.php?category=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have changed the links in my files and removed the .php extension. When I click on a link to the rewritten url to my knowledgebase file everything works fine however I then have further links to the same file with query strings in the url eg:
$link = 'knowledgebase/category/'.$article['catid'];
The problem is once I am on the knowledgebase page links appear as:
http://www.example.com/support/knowledgebase/knowledgebase/category/2
I am pretty sure I need to use a RewriteCond to stop it rewriting but I can't figure out exactly what is required.
Any help would be appreciated.
If I understand correctly, I think this is not a mod_rewrite problem, since mod_rewrite has nothing to do with what links display on the page. Rather, this looks like an issue with the hrefs that are actually in your link definitions (i.e., <a> tags).
The quickest fix might be to add something like:
<base href="http://www.example.com/support/" />
within the <head> section of your pages. That way, if "knowledgebase" is specified within the links, it won't show up twice in the URL for that link.
Let me know if I have this completely wrong.

Resources