Htaccess - Detecting the URL - .htaccess

For my family members I was giving each person their own subdomain
(sister1.mydomain.com, sister2.mydomain.com, etc...)
I was using PHP to detect the domain, and then I'd load information related to the subdomain dynamically.
I'd like to get rid of the subdomains and use the power of .htaccess
My goal is to give the same URL:
www.mydomain.com/sister1
www.mydomain.com/sister2
www.mydomain.com/mommy
www.mydomain.com/daddyo
Obviously, I don't plan to have literal working directories for each person.
I'd pass the "sister1" portion to a process.php script that takes care of the rest.
I've figure out how to do it by manually typing each RewriteRule in my htaccess file:
Options +FollowSymLinks
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteRule ^/?sister1$ process.php?entity=sister1 [L]
RewriteRule ^/?sister2$ process.php?entity=sister2[L]
RewriteRule ^/?mommy$ process.php?entity=mommy[L]
RewriteRule ^/?daddyo$ process.php?entity=daddyo[L]
I feel this is the long way of doing it.
Is there a more universal way of extracting the text after the first "/" forwardslash, and passing it to process.php?entity=$1 ?
I tried it this way:
RewriteRule ^/([A-Za-z0-9-]+)/?$ process.php?entity=$1 [NC,L]
I'm getting the apache 404 error: "Not Found".

It is because you have a mandatory / in the beginning of your rule, i.e., you are always looking for something like /sibling in the URL. Your first examples have that first forward slash as optional due to the question mark after it.
You do not need a beginning forward slash - normally the rewrite rule picks up stuff after the domain name
www.example.com/string/mod/rewrite/gets/is.here
So just remove the starting slash and it should work.

Related

Pattern Matching htaccess conflict

I have migrated my site to new software and am trying to ensure that older links are appropriately redirected to the new url's. I thought I had it working until I received the message from Google regarding increasing 404's.
I seem though to be causing a conflict between the new htaccess requirements and my changes to address the old links.
So the new links look like this:
http://www.exampledomain.com/search/?q=searchterm
And in the htaccess they are picked up like this:
RewriteRule ^search/(.*)$ search.php?q=$1
The above is working as it should.
The old links can look like either of these:
http://www.exampledomain.com/search/searchterm/
or
http://www.exampledomain.com/search/searchterm
I had put this in to the htaccess
RewriteRule ^search/(.*)/$ http://www.exampledomain.com/search.php?q=$1 [R=301,NC,L]
If I don't add the first rule then the new url's just bring up 404's.
If I add second rule the it stops the searchterm being passed and conflicts with the first rule.
Have tried a few things but think there must be an issue with the matching or something else i'm missing.
Any ideas appreciated.
** Added **
So after the first reply I made the change as suggested and it caused a couple of issues but switching the order of the rules has fixed that but has not quite fixed the issue
So now I have this:
RewriteRule ^search/(.*)/?$ http://www.exampledomain.com/search.php?q=$1 [R=301,NC,L]
RewriteRule ^search/(.*)$ search.php?q=$1
The above works for these url's now:
http://www.exampledomain.com/search/?q=searchterm
and
http://www.exampledomain.com/search/searchterm
But for url's like this:
http://www.exampledomain.com/search/searchterm/
it results in this with a trailing slash which prevents the search:
http://www.exampledomain.com/search.php?q=drama/
So just need to remove or not have the trailing slash
I think you are just missing a ? in the rule since the / is optional:
RewriteRule ^search/(.*)/?$ search.php?q=$1 [R=301,NC,L]
Update to address trailing slash:
I'm guessing that the .* is consuming the trailing / before the next rule. To fix that, we need to exclude it from the match:
RewriteRule ^search/([^/]*)/?$ search.php?q=$1 [R=301,NC,L]
Update to address added case where parameter appears after slash:
I'm not sure if you mean literally /search/q=searchstr or /search/?q=searchstr so I will attempt to address both cases.
If it's the latter, which is a true query string, place this rule above the first using QSA in order to pass the query string along to the new URL:
RewriteRule ^search/$ search.php [R=301,NC,QSA,L]
To address the first variation (without the ? query string), you will need to place this rule above the first, which literally looks for the q=:
RewriteRule ^search/q=(.*)$ search.php?q=$1 [R=301,NC,L]
And since I get the feeling you'll update this question again to ask about what happens if there is a trailing slash, I'll go ahead and modify that rule to handle this case as well:
RewriteRule ^search/q=([^/]*)/?$ search.php?q=$1 [R=301,NC,L]
If these rules still don't solve every case for you, then you're dealing with some really bad code from your previous URL and I feel very sorry for you. :P

htaccess 301 redirect while replacing characters in URL - Helicon ASAPI ReWrite module

I have an old site that is being rebuilt. Instead of using a folders structure, it is using sub-domains. The segments are different, but the redirect itself is pretty simple. I can handle it like so:
RewriteRule ^segment/blog/view$ http://blogs.site.com/segment/article [R=301,NE,NC,L]
RewriteRule ^segment/blog$ http://blogs.site.com/segment [R=301,NE,NC,L]
So if I had www.site.com/segment/blog, it will now go to blogs.site.com/segment.
If I had www.site.com/segment/blog/view/catchy_name_goes_here, currently it redirects it to blogs.site.com/segment/article/catchy_name_goes_here and I NEED it to go here: blogs.site.com/segment/article/catchy-name-goes-here.
My issue comes from a decision to change the separator in the URI. The old articles were built with underscores '_' and the new articles are built with hyphens '-'.
How can I replace the underscores in the article titles with hyphens?
Try these rules:
RewriteRule ^/segment/blog$ http://blogs.site.com/segment [R,I,L]
# replace _ by - repeatedly
RewriteRule ^(/segment/blog/view)/([^_]*)_+(.*)$ /$1/$2-$3 [I,N,U]
# all _s gone, now do a redirect
RewriteRule ^/segment/blog/view/([^_]+)$ http://blogs.site.com/segment/article/$1 [R,I,L,U]
I ended up having to use the following. I don't know how many people this might effect due to the unique settings for this site in particular, but thought I would post the answer so it could help anyone that might need it.
The full settings on this are a server running IIS with Server 2k. The site consists of several static content pages, vb script, classic ASP, dot Net, and this is all intertwined with ExpressionEngine pages. It's a mess to say the least. To top it off, Helicon Tech's ASAPI Rewrite Module version 3 is running on the server for .htaccess usage. No sub-expressions, groupings, etc. were taking or being followed/processed. The index.php rule was getting bypassed as well.
This all said, I ended up with the following which parsed everything I needed.
RewriteRule ^index.php/segment/blog/view/([^_]*)_+(.*)$ http://www.site.com/index.php/segment/blog/view/$1-$2 [R,NC]
RewriteRule ^index.php/segment/blog/view/([^_]*)$ http://blogs.site.com/segment/article/$1 [R=301,I,L,U]
RewriteRule ^segment/blog$ http://blogs.site.com/segment [R=301,NE,NC,L]
RewriteRule ^/segment/blog$ http://blogs.site.com/segment [R,I,L]

erase part of a URL using htaccess rewrite

i need to remove part of Joomla/Virtuemart generated SEF URI using .htaccess
the URI represents a menu hierarchy and structured this way:
online-store
- inner-store
-product-catalog
this is the resulting URI:
www.domain.com/online-store/inner-store/product-catalog
i would like to change it to:
www.domain.com/online-store/product-catalog
thought this might help but its not making any difference
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^online-store/inner-store/\d+-(.+) /online-store/$1 [R=301,L]
i know its not considered good practice but i can't change the menu structure.
any suggestions ?
This regex \d+-(.+) will match 1 or more digits followed by hyphen followed 1 or more any thing
Try this code instead:
RewriteRule ^(online-store)/inner-store/(.*)$ /$1/$2 [R=301,L,NC]
Make sure this is first rule in your .htaccess and use a different browser to test it to avoid caching issues.

Dynamic URL rewrite with .htaccess

Migrating a site from IIS to Apache and I'm having trouble with a RewriteRule I can't find the answer to.
I need to redirect from www.domain.com/pubs/books/P123.asp to www.domain.com/books.php?p=P123.
No matter what I try, I'm getting a 404.
Can someone give me a pointer? All the examples I seem to find have the dynamic part of the URL at the end. Do I first need to strip the .asp?
Drupal is also running on this site so there is some RewriteRules there to.
The latest I have tried is: RewriteRule /pubs/books/P(.*)$.asp /books.php?p=$1
First of all: Read the documentation! You can find it here.
It says:
Syntax: RewriteRule Pattern Substitution [flags]
and
In VirtualHost context, The Pattern will initially be matched against
the part of the URL after the hostname and port, and before the query
string (e.g. "/app1/index.html").
In Directory and htaccess context,
the Pattern will initially be matched against the filesystem path,
after removing the prefix that led the server to the current
RewriteRule (e.g. "app1/index.html" or "index.html" depending on where
the directives are defined).
If you wish to match against the
hostname, port, or query string, use a RewriteCond with the
%{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables
respectively.
(in other words: A pattern starting with a slash will never match if in .htaccess context)
First of all, make sure mod_rewrite is enabled and the rewriteengine is on (put RewriteEngine on in your .htaccess) and allowed there (see docs).
Assuming this is in a .htaccess in your www-root, this should work:
RewriteRule ^pubs/books/P(.*)\.asp$ books.php?p=$1 [L]
The syntax ^...$ makes sure that this is the entire filesystem path. We wouldn't want to match http://example.com/some/more/paths/pubs/books/P123.asp here and rewrite that.

htaccess Rewrite subdirectory to php page

I've successfully rewritten
RewriteRule ^alphabetical alpha.php
So that when anyone comes to www.example.com/alphabetical it loads alpha.php. However, I'm having problems creating a rule so that when someone comes to www.example.com/alphabetical/a it should loa alpha-a.php. This is the rule I've tried that isn't working:
RewriteRule ^alphabetical/a alpha-a.php
put your second more specific rule on top of the the first rule.
RewriteRule ^alphabetical/a alpha-a.php
RewriteRule ^alphabetical alpha.php
further, you can use a $ at the end to denote an end of url, saying urls ending with 'a'.

Resources