Edit way directory is invoked on server - .htaccess

What I'm trying to do seems very simple, but I did not yet find the solution. The idea is that a specific directory on my server can only be opened through one URL. Currently, the website can be accessed by navigating to http://dir.mydomain.com as well as http://mydomain.com/dir. All links use the first option, but to prevent search engines from detecting duplicate content and my users from getting confused I'd like to simply disable the second option. I would swear that I have read something about a solution some time, but I can't remember it or find it back... Hope you can help me fixing this, thanks in advance!

Why not just redirect with a 301 to dir.mydomain.com so whatever links to the 2nd get mapped to the 1st?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /dir
RewriteRule ^dir http://dir.mydomain.com [R=301]

Related

.HTACCESS RewriteRule force default page to HTTP

I did a lot of searching on this, and just couldn't quite find the right answer. A lot of things got me close, but nothing worked like I wanted it to.
I'm working on a site where, because of an external JQuery add-on library, I need to force certain pages to be HTTP. Certain other pages (for shopping, etc) need to be HTTPS.
So far in my .HTACCESS file I have:
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^(index|event)\.php$ http://www.example.com%{REQUEST_URI} [R=301,L]
This works perfectly when the user goes to
https://www.example.com/index.php
But doesn't redirect when they go to
https://www.example.com/
Any suggestions on how to catch that last instance?
Thank you in advance!
To match landing page tweak your regex to match empty URI also like this:
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^((index|event)\.php)?$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC,NE]

htacces - need to fix broken links coming from other sites to mine

I am having an issue where Google Webmaster Tools is reporting a ton of 404 links to my site which are coming from ask.com.
I have tried to get ask.com to fix their side but of course they are not, so now I am stuck with over 11k of bad links to my site which I am suspecting is effecting my ranks right now.
Anyways I have a possible way to 301 them, but not sure how to do it with .htaccess.
Here is the bad link pointing to my site
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/about.php
It should be
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/
Besides the about.php there are other variations of endings as well, I basically need to be able to remove the ending.
Problem is that the URL after /sentence-examples/ can change. The beginning is always:
http://www.freescrabbledictionary.com/sentence-examples/
So basically:
http://www.freescrabbledictionary.com/sentence-examples/<-keep but can change->/<-keep but can change->/<-remove this->
This .htaccess should be placed on the folder before sentence-examples:
RewriteEngine on
# Redirect /sentence-examples/anything/anything/remove to /sentence-examples/anything/anything/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(sentence-examples/[^/]+/[^/]+)/.* [NC]
RewriteRule ^ /%1/? [R=302,PT,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /sentence-examples/examplesentence.php?havethis=$1&word=$2 [L]
Change 302 to 301 once you confirm it's working as expected.
If you have a CMS installed you might need a different rule to work along with it without conflicting.
Keep in mind that if you had previously tried different redirects using 301 aka permanent redirect its recommended that you use a different browser to test this rule to avoid the caching.
This is possibly quick and dirty but I've done a simple test on localhost and here just to make sure it works.
RewriteEngine On
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)\.php http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]
You can see that I've added wildcard groups (.*) to the RewriteRule so that we can pick up the elements of the URL that we need to aid in proper redirection i.e. $1 and $2. You can also use the third one ($3) to get which destinations are being targeted alot for your SEO needs.
NB: The rule above assumes that that the redirected URL will always be from a .php target and to ensure that you can redirect regardless of whatever comes after the 3rd URL segment replace the RewriteRule with this
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)$ http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]

Complex URL rewriting with .htaccess

Here is what i am trying to achieve
www.example.com/category
www.example.com/category/subcategory
www.example.com/category/subcategory/product
www.example.com/static-page (like /about-us, /contact, /our-services)
(category, subcategory, products, static-pages etc are dynamic text and there is a permalink foreach thing in the database)
if you see all above requests, you will notice that they are just like extending the directory sturcutre one step each time when the link is clicked, e.g, first was category, and then when I clicked on the subcatogery, I was sent to category/subcategory/ and the finally to product page
Anybody can help me how to acheive all this, i have tried lot to achieve this but in vain yet.
Currently i am using this .htaccess
RewriteRule c/(.*)/(.*)/ cat-details.php?permalink=$1&subcat=$2
RewriteRule c/(.*)/(.*) cat-details.php?permalink=$1&subcat=$2
RewriteRule news/(.*)/(.*)/ news-detail.php?news-id=$1&permalink=$2
RewriteRule news/(.*)/(.*) news-detail.php?news-id=$1&permalink=$2
RewriteRule d/(.*)/(.*)/(.*)/(.*)/ download-detail.php?download-id=$1category=$2&subcategory=$3&permalink=$4
RewriteRule d/(.*)/(.*)/(.*)/(.*) download-detail.php?download-id=$1category=$2&subcategory=$3&permalink=$4
As you can see i have to add c/, news/, d/ etc for each link, i am trying to get rid of this and want to make links pretty whitout proceeding c/, news/, d/ etc
About the only way you're going to be able to do this in Apache proper, is to provide info on which URLs map to which scripts. Apache lets you do this via the RewriteMap directive, but that won't work in .htaccess files. Without it, and without the news/ or c/, Apache doesn't have enough info to route the URLs properly.
What you could do, is simply rewrite every URL that doesn't refer to an existing file, to a script that knows which scripts to run in response. A "router" or "front controller" script, they call it. Something like this:
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule (.*) router.php?path=$1 [QSA]
And in the script, you examine $_GET['path'], decide which script should handle the request, and load/include/require it.
I have found a way myself, just posting it if it could help anyone else in trouble just like me :p
RewriteRule ^([^/]*)\/$ /romuniverse/cat.php?permalink=$1 [L]
RewriteRule ^([^/]*)/([^/]*)\/$ /romuniverse/sub-cat.php?permalink=$1&subcat=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.rar$ /romuniverse/download-detail.php?download-id=$1&category=$2&subcategory=$3&permalink=$4 [L]

.htaccess RewriteRule not redirecting from real folder

I'm still a bit fuzzy on the working of .htaccess, and I've looked around but I can't find anything to help this specific issue.
EDIT: I realize there are other questions that seem like they cover this issue, but I checked some and they didn't seem to offer any help I could understand, and I didn't want to hijack them with my own issues.
This is what I have:
Options +FollowSymLinks
#RewriteBase /
RewriteEngine on
RewriteRule /mp3/(.*) http://old.domain.com/mp3/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
As you can see from the last line, the string typed after the server name is actually a URL parameter and depending on that parameter, different content is pulled from the database and that page is displayed on the site.
The problem I'm having is that the client has a content page called "podcast", so they would go to site.com/podcast which should quietly redirect to site.com/index.php?=podcast and load the content for that page. Unfortunately, the client also has a real site.com/podcast/ folder on their server. Because of this, the rewrite is ignored and the browser attempts to load that folder. It either shows a file listing or a forbidden error if I disable the listing.
After some research (I'm still new to htaccess), I learned that the two lines prior disable the rewrite if the path points to an actual file or folder. Unfortunately, commenting out the one with !-d doesn't seem to have any effect, and commenting out both gives me a server error.
Admittedly, part of the problem here was lack of foresight. URL rewrites should have been planned before everything else was put together, but it wasn't until the site was basically completed that I was notified that the client wants "Friendly URLs" that don't include the ?p= part. Regardless, perhaps there is a way to fix this.
Is there some .htaccess trickery I can use that will force the rewrite even if the URL entered points to a folder (not a specific file) that actually exists? As stated before, removing the !-d doesn't seem to help, although I'm not sure why. Perhaps I misunderstand its purpose.
Thank you for any help, and please be lenient with me if I overlooked something obvious. This is an issue presenting itself on the client's live site right now so I feel a little rushed in solving it. Thanks again.
OH YEAH, and the solution can't be specific to /podcast. The way the client's site is set up, when they want to create a new subpage for the site, a new name is saved for that content based on their title for the page and it is possible (unlikely, but still possible) that another page can be created with a name that matches an existing folder on the server.
Here is a note from mod_rewrite documentation:
By default, mod_rewrite will ignore URLs that map to a directory on
disk but lack a trailing slash, in the expectation that the mod_dir
module will issue the client with a redirect to the canonical URL with
a trailing slash.
This explains why mod_rewrite ignores the URL /podcast. I would suggest that you rename physical directories so that do do not (accidentally) match article names.
Another option would be to disable the DirectorySlash setting. This will prevent Apache from redirecting /podcast to /podcast/.
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
Be warned that disabling this setting has side effects. Read through the documentation first.
Change the following line of code:
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
to
RewriteRule ^(podcast([^?]*)) index.php?p=$1 [L,NC]

Redirect dynamic subdomain to same subdomain with subpage. How?

I'm a little stuck in here. I need to get some help with this subdomain-situation.
I need to redirect http://dynamicsubdomain.example.com/ to
http://dynamicsubdomain.example.com/account/welcome.html.
How do I do this? I tried several things but all without result. The main problem is that I can't fetch the entered dynamic subdomain from the %{HTTP_POST} string from mod_rewrite.
Another issue would be that it's creating and endless loop. So it only needs to redirect on these conditions, not when there's a URL like http://dynamicsubdomain.example.com/test/page.html. Because else it will create and endless loop. It's just for the starting page from the website.
I hope y'all can help me out, it's one of the last but important things from my project.
Thanks in advance!
There are several options on the URL redirection wiki page. For example, how about dropping an index.php in the root that redirects to the destination?
header("Location: http://dynamicsubdomain.example.com/account/welcome.html");
Why does the domain matter so much if you are staying on the same domain, and just redirecting to a different path?
The UseCanonical setting in Apache may have an effect on this, but it is defaulted to on, which preserves the host and port specified in the request.
RewriteRule ^/$ /account/welcome.html [R,L]
Hey guys, thanks for your support/help but I found the solution myself. Quicker than I thought :)
This is what does the trick, I hope I can help someone with this:
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{HTTP_HOST} ^([A-Za-z0-9]+).example\.com [NC]
RewriteRule ^ http://%1.example.com/account/welcome.html [L]
#gahooa: I need to do this because my mainpage example.com is just a sort of landing-page with no special things. The extra part of the URL "account/welcome.html" is for showing a page related to the "subdomains"-account (gahooa.example.com for example will show your profile page). In my app I catch up the subdomain by a preg_match so it knows witch account it has to load. I hope I'm clear :) But thanks anyway!
This is my first time i'm using Stackoverflow but it actually works great! Quick replies from experts, great work! I definitely will come back :)
If you really want to use HTTP_HOST:
RewriteRule ^$ http://%{HTTP_HOST}/account/welcome.html [L,R]
But like gahooa already said you don’t specify an absolute URL if you stay with the same host. The URL path will suffice:
RewriteRule ^$ /account/welcome.html [L,R]

Resources