htaccess mod_rewrite on subdomain with subdirectories - php

I know there are TONS of questions on this website regarding this topic, as well as TONS of tutorials around google. However I have searched for a while now and I cannot seem to find one that explains my specific situation.
I am building a social application in which the whole application is being stored in a directory of a subdomain the website like so: subdomain.example.com/network/ <-- network being the directory of all the application files.
I have my .htaccess file located in the root of the subdomain and as of right now I have it removing .php from all the files and that is working throughout the entire project as it should. I have a user.php page which each user's profile is based off and the unique identifier for each user is their username (not 'id' like most people use.) So right now the urls look like /network/user.php?username=TylerB which is not very pretty. I have tried many different things to get this to look like /network/user/TylerB but nothing I try seems to work. I don't know if it's because it's in a directory, a subdomain or what. As of right now, when I have /network/user/TylerB (TylerB is my username) it gives me a 404 error.
Here is my current .htaccess file:
RewriteEngine On
RewriteRule ^user/([^/\.]+)/?$ user.php?username=$1 [L]
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]

I would recommend setting the RewriteBase directive and matching on the full pattern, making sure to rewrite to the /network/ directory.
RewriteEngine On
RewriteBase /
RewriteRule ^network/user/([^/\.]+)/? /network/user.php?username=$1 [L]
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
Using the testing tool at http://htaccess.madewithlove.be/ I get an output of:
This rule was met, the new url is http://example.com/network/user.php?username=name
The tests are stopped because the L in your RewriteRule options

Related

Google indexing two URLs for the same page - with and without index.php

I am new to the trade of Web development. Currently working on five content based websites of a customer. The sites are designed using Laravel 4 and use a shared hosting with no access to configurations. I am required to remove 'index.php' from all the article pages and make them clean URLs for SEO purpose. I am facing two problems. The problems seem to be similar in nature hence quoting both in same question.
For Addon domains - I changed the document root of the domains to the 'public' folder of Laravel. Then added the code below in .htaccess file to make the URLs cleaner without the 'index.php' part
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?/$1 [L]
Though the code works fine if URLs are entered directly to browser's address bar. The problem is that now when I search my site in Google.com using site:mysite.com it shows some of the pages without the index.php part in the URL while some of the pages with index.php in the URL. The worst thing is that some of the the pages appear twice - once with index.php in URL and second time without index.php
e.g. the search result would contain pages with URLS like this -
www.mysite.com/index.php/article-1-content
www.mysite.com/article-1-content
If I am not wrong this would result in duplicate content. Moreover when I open the article page through the URL having index.php, all the other URLs on that page - like internal links and side bar articles also appear with index.php in their URL. What should I do to completely remove index.php from URLs and google index. Please help.
Sorry for making the question too long but my second problem seems to be of similar nature -
For primary Domain - I have added whole of the site code in public_html and then added this piece of code to .htaccess file in public_html to change the document root
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^primarydomain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.*)$ public/$1 [L]
and also code to remove index.php part in .htaccess file of public folder - (same as that in addon domains ). The problem is that on searching in Google each and every article page appear twice with URLs looking something like this
www.primarydomain.com/article-1-content
www.primarydomain.com/public/index.php/article-1-content
google is indexing the URL with 'public/index.php' part as well. What should I do to completely get rid of these not so clean URLs.
Thanks for tolerating such a long question :) any help would be appreciated.
Regards.
This sounds like a job for a canonical tag.
As long as the URL loads, the canonical tag will do the rest (ie you can specificy which URL to use).
https://support.google.com/webmasters/answer/139066?hl=en

How to redirect all API requests using .htaccess, while keeping asset requests intact?

TL; DR: I would like to hit the index-api.php file if api is found in the URL, but then simply keep all other requests pointing to the site/dist directory as if it were the 'root' of the site.
So, I've spent way too many hours on this and trust me, I've dug through all of the resources for mod_rewrite. I guess I'm just not quite understanding and figured I'd ask on here.
What I want to do, in theory, seems simple. I'm building a single page application (Angular App) using Grunt, outputting that to a the root of a WordPress install. The WordPress install is simply serving up an API using the WordPress JSON API plugin, so I want the root of the site to hit my Grunt directory (located at site/dist/index.html), but all requests to siteurl.com/api to hit the index.php file and proceed normally.
Keep in mind I have other assets / images located in this site/dist directory, so ideally, it would be awesome if all requests to the site root would simply use this folder as the "base" of the site (e.g. a request to siteurl.com/images/testimage.jpg pulls from site/dist/images/testimage.jpg).
I feel like I'm onto something here and am surprised I couldn't find anything that directly tackles this issue.
What I've done now is renamed the index.php from WordPress to index-api.php and left it the same:
index-api.php:
<?php
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wordpress/wp-blog-header.php');
// phpInfo();
.htaccess:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ index-api.php [L]
RewriteRule (.*)$ site/dist/index.html [L]
</ifModule>
I tried a myriad of other efforts from a few posts trying to get this working, and it seems to me like it should work fine. The funny thing is, if I comment out the last line RewriteRule (.*)$ site/dist/index.html [L] the api request works normally as expected, so I know I'm close.
Any suggestions?
Would appreciate anyone's help on this, it's been really confusing!
In the first place you'll need to make sure that requests made to /index-api.php are not matched and rewritten by the second rule. In the second rule you can use $1. $1 will be replaced with whatever was matched in the first capture group. We'll also need to make sure that the second rule will not match what it rewrites, or we'll end up with an infinite loop and an internal error.
You can use the $1 in the first rule too, as I show below:
RewriteRule ^api/(.*)$ index-api.php?url=$1 [L]
RewriteCond %{REQUEST_URI} !^/site/dist/
RewriteCond %{REQUEST_URI} !^/index-api\.php
RewriteRule (.*)$ site/dist/$1 [L]
I recommend reading the documentation of mod_rewrite to get a better understanding how you can use it and what things you have at your disposal while rewriting url's.

htaccess rewrite ".../pages/about.php" to ".../about"

I've searched and found a lot of questions on this site and elsewhere that are very similar, but I've tried implementing and modifying all the suggestions I've found and none of it works. I realize this is a very basic question an I am extremely frustrated because nothing I'm trying is working.
With that having been said... I am trying to organize my content pages within kurtiskronk.com/pages/... (e.g. kurtiskronk.com/pages/about.php)
What I want to do is make it so that I can simply link to kurtiskronk.com/about ... So how do I go about stripping "pages/" and ".php"? I don't have a ton of content pages, so it's not a big deal if I have to specify for each page, though something dynamic would be handy.
NOTES: I am using Rackspace Cloud hosting, and WordPress is installed in /blog. My phpinfo() can be seen at http://kurtiskronk.com/pages/phpinfo.php
This is my existing .htaccess file (in the root)
php_value register_globals "on"
Options +FollowSymLinks
RewriteEngine On
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
RewriteBase /
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [L]
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
# PHP - MAIL
php_value mail.force_extra_parameters -kurtis#kurtiskronk.com
I tested and the rewrite works with the line below (/about as URL brings up file /pages/about.php), but then the homepage gives a 500 Internal Server Error:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
So I'm still sort of in the same boat as before, and as a follow-up, possibly more difficult question, if you go to http://kurtiskronk.com/weddings I am using SlideShowPro (flash) w/ SSP Director (self-hosted) as the back-end for it. When it pulls up a new image, it adds the following after /weddings ... "#id=album-152&num=content-9698"
There are four sections of the portfolio
# Homepage (kurtiskronk.com) id=album-148 ($id is constant for this section)
# Weddings (/weddings) id=album-152 ($id is constant for this section)
# Portraits (/portraits) id=album-151 ($id is constant for this section)
# Commercial (/commercial) id=album-150 ($id is constant for this section)
Assuming we get kurtiskronk.com/weddings to rewrite successfully without breaking anything, how would we make the total URL something cleaner kurtiskronk.com/weddings/9698 since the $num is the only thing that will change within a given section?
Kurtis, thanks for the extra information. It's a lot easier to give a specific answer to this.
My first comment is that you need to separate out in your thinking URI space -- that is what URIs you want your users to type into their browser -- and filesystem space -- what physical files you want to map to. Some of your mappings are URI->URI and some are URI->FS
For example you want to issue a permanent redirect of www.kurtiskronk.com/* to kurtiskronk.com/*. Assuming that you only server the base and www subdomains from this tree, then this cond/rule pair should come first, so that you can assume that all other rules only refer to kurtiskronk.com.
Next, you need to review the RewiteBase documentation. .htaccess files are processed in what Apache calls a Per-Directory context and this directive tells the rewrite engine what to assume as the URI base which got to this directory and .htaccess file. From what I gather, your blog is installed in docroot/blog (in the filesystem, and that you want to get to directory by typing in http://kurtiskronk.com/blog/ but that this .htaccess file is for the root folder -- that is the base should be (this goes before the www mapping rule)
DirectorySlash On
DirectoryIndex index.php
RewriteBase /
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
You can add some field dumps look for REDIRECT_* in the Server or Environment table in the phpinfo O/P to see if these are sensible. For example:
RewriteWrite ^(.*)$ - \
[E=TESTDR:%{DOCUMENT_ROOT}/pages/$1.php,E=TESTPDR:%{DOCUMENT_ROOT}/pages/$1.php]
Your next rule is that if the file exists in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [NS,L]
[Note that some shared service sites don't set up DOCUMENT_ROOT properly for the rewrite engine so you may need to run a variableinfo script (<?php phpinfo(INFO_ENVIRONMENT | INFO_VARIABLES); to see if it sets up alternatives. On your site you have to use %{ENV:PHP_DOCUMENT_ROOT} instead.]
Your next rule is that if the file exists, but with the extension .php in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [NS,L]
Now redirect any blog references to the blog subdirectory unless the URI maps to a real file (e.g. the blog stylesheets and your uploads.)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
A complication here is that WP may be using a poorly documented Apache feature call Path Info that is a script can act as a pseudo directory so http://kurtiskronk.com/blog/tag/downtown/ is redirected to docroot/blog/index.php/tag/downtown/ which is then executed by `docroot/blog/index.php using /tag/downtown/ as the PATH_INFO. But this is one for Wordpress experts to comment on. If this last rule doesn't work then try:
RewriteRule ^blog/(.*) blog/index.php/$1 [L]
PS. I like your site. I wish I was that young again :(
Postscript
When you say "it doesn't work", what doesn't with this .htaccess?
http://kurtiskronk.com/phpinfo,
http://kurtiskronk.com/phpinfo.php,
http://kurtiskronk.comblog/tag/downtown/
It's just that these rules work for these tests (with domain swapped) on mine. (One way is to move or copy the above variableinfo.php to the various subdirectories. If necessary temporarily rename the index.php to index.php.keep, say, and copy the variableinfo.php to the index.php file. You can now enter the various URI test patterns and see what is happening. Look for the REDIRECT_* fields in the phpinfo output, and the SCRIPT_NAME will tell you which is being executed. You can add more {E=...] flags to examine the various pattern results. (Remember that these only get assigned if the rule is a match.
Lastly note the changes above especially the additional NS flags. For some reason mod_rewrite was going directly into a subquery which was resulting in redirect: being dumped into the file pattern. I've had a look at the Apache code and this is a internal botch to flag that further redirection needs to take place (which then replaces this or backs out). However this open bug indicates that this backout can be missed in sub-queries and maybe that's what is happening here. Certainly adding the NS flas cured the problem on my test environment.
PS. Note the added explicit DirectoryIndex directive and also that whilst http://kurtiskronk.com will run the root index.php, the explicit /index.php version will run the one in pages, because that's what your rules say.
Here is a simple solution. You can use it apache conf file(s) or in .htaccess (easier to set up when you're trying).
mod_rewrite has to be enabled.
For example, use .htaccess in your DocumentRoot with:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
It will redirect /about to /pages/about.php, and any other page.
The "RewriteCond" part is to authorize access to an existing file (eg: if you had an "about" file at the root of your site, then it will be served, instead of redirecting to /pages/about.php).
Options +FollowSymLinks
RewriteEngine On
RewriteRule /([0-9]+)$ /pages/$1.php [L]
Put something like this in your .htaccess file. I guess that is what you want.
Juest a redirect from a simple url to a longer url.

.htaccess: how do multiple .htaccess files work?

This is in context of CakePHP, but I'm sure it is common in other applications. I implemented the instructions on this page:
http://book.cakephp.org/view/917/Apache-and-mod_rewrite-and-htaccess
A:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
B:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
C:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
In section 3, there are 3 .htaccess files. Let's just call them A, B, and C in order for simplicity. Although it seems to be working on my server (localhost XAMPP), I'm not sure if I understand the path of the redirects. Whatever address I type in, it always go to localhost\cake\users\login as it's supposed to.
Root directory of my application in terms of where it resides is \cake. I have \cake, \cake\app, and cake\app\webroot with A, B, and C, respectively, in them. After following the above instruction, the codes in cake\app run, which is what I want. Prior to setting up the .htaccess files correctly like above, codes in \cake were being run; this was incorrect as it was telling me my database wasn't setup, you don't have this file, etc.
Can someone chime in and help me out with the "path" this application is taking before running any php files? Is it redirecting everything to cake\app\webroot, then to whatever redirect is setup beyond the .htaccess files from there? (in this case, to cake\users\login)
Thank you for your help!
PS: I'm ashamed to say I still haven't figured out which part of which file is redirecting to all of the HTTP request to the user login page. I set this up a while ago, and I just came back to it. I think I was following the second tutorial on the CakePHP Cookbook page where you add an ACL feature..
Can someone chime in and help me out with the "path" this application is taking before running any php files? Is it redirecting everything to cake\app\webroot, then to whatever redirect is setup beyond the .htaccess files from there? (in this case, to cake\users\login)
In short, yes. There is only one .htaccess file doing the real work, and that is "C" (which is the file in /app/webroot/.htaccess). This passes any requests that aren't for an existing file or directory to index.php which bootstraps CakePHP and handles the request. Any further "redirection" is handled by CakePHP's routing.
Edit:
To address your question about what's redirecting to the login page, chances are you have configured the Auth component and haven't set any "public" pages via $this->Auth->allow().
The other two are there in case you put the CakePHP installation directory or the app directory into a folder that is used by Apache to serve pages, e.g. /var/www/html or similar. You'll note that the structure is:
/cakephp-1.3.x/.htaccess ("A")
/cakephp-1.3.x/app/.htaccess ("B")
/cakephp-1.3.x/app/webroot/.htaccess ("C")
So, any request at any of these levels will end up being handled by the RewriteRule in "C". This is done to protect sensitive data such as your database connection information and ensure the application functions properly (as all requests should go through the CakePHP bootstrapper, unless you've set up custom routing).
The following line redirects everything to your index page, which is a Cake construct.
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
In your case, I don't think that your redirections are directly caused by your .htaccess files. Your webroot htaccess simply pipes everything to your index page.
You should consider checking your index page, however. The controller for that page probably has a default clause for incoming arguments. Thus, if you enter a url with an unknown parameter, your index page will simply show a login screen.
That's my two cents. However, I've only worked with CakePHP as an experiment, so someone with more experience should feel free to correct me if I'm wrong.
I think it's occurring due to use of Auth component in your application.You wrote that you were trying on Acl component part of cakephp, which is where Auth component is used.In case of inclusion of Auth component in requested controller, cakephp redirects to /users/login by default if no other loin method is specified. So i guess htaccess is not causing problem here.

Using mod_rewrite to redirect subdirectory to webroot with CakePHP app

Hey all, I have the following setup in my webroot:
/issues
(Trac install)
/myapp
(Cake install)
What I want to happen is that when I go to http://example.com/, it will pull up the cake app - basically, redirecting ^(.*) to myapp/$1 (but invisibly - the URL for users/login is http://example.com/users/login). I have an exception for /issues/, which works fine - when I go to /issues/, it pulls up Trac. But my Cake app keeps redirecting things to http://example.com/myapp/users/login, which I think is breaking my sessions, and in any case is not desirable. I've tried to force such URLs back to the root using something like
RewriteRule ^myapp/(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond $1 !^(myapp|issues)/.*$
RewriteRule ^(.*)$ myapp/$1
...but to no avail. I also have RewriteBase / in all of my CakePHP .htaccess files.
Any ideas as to how I can actually get this to do what I want it to?
it looks that this link could help you. After that you should have a cleaner separation, in my opinion.

Categories