Tricky: Remap url but stay on the same url internally - .htaccess

Need your help. Just spend many ours on this htaccess problem and still don't have a clue how to manage this.
I have many http://www.example.com/menu-alias/foo links on my company's website which should get redirected to http://www.example.com/foo.
This alone shouldn't be the hard part but listen up... the tricky part follows.
I don't manage to get the site (Joomla 1.5) working without the 'menu-alias' this means that all http://www.example.com/foo should get internally mapped to http://www.example.com/menu-alias/foo. So that the user still has http://www.example.com/foo in his browser's address bar.
To make it even more complicated i have to 301 redirect the old menu-alias/foo links to /foo.
Can some htaccess guru help me out? Is this even possible?

You can try adding these rules in the htaccess file in your document root (or vhost config):
RewriteEngine On
# externally redirect requests that have "menu-alias"
RewriteCond %{THE_REQUEST} /menu-alias/([^\ \?]+)
RewriteRule ^ /%1 [L,R=301]
# internally rewrite requests back to menu-alias
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/menu-alias/
RewriteRule ^/?(.*)$ /menu-alias/$1 [L]
Couple of potential problems:
Joomla may be looking for the original un-rewritten request in $_SERVER, if so, the rewrite won't work.
The rule to add the /menu-alias/ back into the URI does so blindly rewrites all requests that don't point to an existing resource. This means "virtual" paths that Joomla may handle will get a "menu-alias" appended to the front.

Related

Htaccess and redirecting from https page to non https page

I have a website where all URLs are rewritten to avoid seeing extensions and the original file name. So, in my htaccess, I have the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^example-1$ filename_1.php
RewriteRule ^example-2$ filename_2.php
Now, I also have a few pages that I wanted to be only https and only http. So, let's say I want filename_1 to alwaysbe https and filename_2 to always be http. So, I have the following codes.
RewriteCond %{HTTPS} off
RewriteRule ^(filename_1)\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]
And
RewriteCond %{HTTPS} on
RewriteRule ^(filename_2))\.php$ http://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]
This is just and example but I actually have many files affected by those two rules.
Now, I thought it was working great until I noticed that when I am on an https page and I access a non https page, the url changes to show the actual file name. So, if I am on https://www.mywebsite.com/example-1 and I try to access example-2, the url changes to http://www.mywebsite.com/filename_2.php instead of http://www.mywebsite.com/example-2. And the same for going from http to https. (I am keeping it simple in those examples but the problem only arise with pages with variables in the url. Simple ones like www.mywebpage.com/homepage work just fine.)
So, I added this line of code:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(example-2)/ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301,NE]
So, it worked to go from https to http but then things just got crazy! I get redirect loops at certain https and non https page and I have no clue why! The thing is that I started by adding one page at a time to this new rule. And pages that I haven't even added yet give me some redirect loop. Then I try adding those pages but instead of https on, I used https off and it got even crazier.
So my question is what is a good way to have all the following perform harmoniously:
1. Rewrite all urls to custom ones
2. Having http and https page while keeping the custom urls and avoiding all redirect loops.
Thanks!

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]

rewriting versus redirecting with mod_rewrite for pretty URLs

I'm using a standard snippet to route requests for "myfile" to "myfile.php", using Apache:
# Add .php-extension to base URLS using rewrite (so links can omit it)
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
and it works. When I type "mydomain.com/myfile" in the browser, I see the page load, with no ".php" in the URL bar. Great.
But now it's also quite clear that I can still continue to still view the page at the old location "mydomain.com/myfile.php". And there are a few problems with that:
1) SEO: I'm diluting link juice, splitting the content between two URLS, one with ".php" and one without. Not crucial, but hey...
2) Any existing public links to the site out in the world will continue to see the unprettified URLs. Yech.
3) Most importantly, all my existing internal navigation links
will continue to point to the unprettified URL. Dealbreaker.
My head starts to spin at this point. I think what I want is to rewrite the non-".PHP" version to the ".PHP" on the server, but also redirect the old links with the extension to the extension-less content. Is a there a standard, "best practices" way around this conundrum? Should go through my navigation and strip out all the ".php" file extensions from the links to get rid of the extensions once and for all? Seems like the wrong path, but it's occurred as a possibility :) Even more severely, would it be safe to have PHP files saved without extensions in a LAMP stack? (I've never even tried, too conditioned to avoid it). How do I make the transition to pretty URLS complete?
Thanks in advance for any guidance.
Short Term
For all of your non-pretty links have a RewriteRule to redirect to their corresponding pretty link with status=301 (permanent redirect). That way search engines will start caching your new pretty links while gradually discarding all of your old non-pretty links. Here is the .htaccess stuff for that part:
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo with 301
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]
Long Term
Consider converting your internal non-pretty linkes with pretty links (remove \.php).

mod_rewrite does not work

I have the following page name
http://example.co.uk/vehicle.php?size=large
and I have written the following rewrite rule so the domain should look as follows
http://example.co.uk/size/large
Here is the rule
RewriteEngine On
RewriteRule ^size/([a-zA-Z0-9]+)/$ vehicle.php?size=$1
I have two problems:
The first is its not working. In fact its not working at all.
The second is if it does work then it will remove the page name which I don't want. I'm struggling to see my mistake in the first place to fix it but I want the domain to look as follows:
http://example.co.uk/vehicle/size/large
If you want to redirect from /vehicle.php URLs to /vehicle/ URLs, then try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*?)=(.*?)$
RewriteRule ^vehicle\.php /vehicle/%1/%2? [R=301,L]
As for displaying contents of nonclean URLs (like /example.php?foo=bar) at clean URLs (like /example/foo/bar), there is a cyclic-redirection issue when trying to use both internal (from a to b) and external (from b to a) redirections at the same time.
If you've decided to switch to clean URLs, then I would recommend you to use /index.php as the only handler for all requests, and use mod_rewrite solely to redirect from old URLs to clean ones. Moreover, I usually perform most of operations with PHP (by parsing $_SERVER['REQUEST_URI'] via PHP), and use mod_rewrite solely to map all requests to index.php file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,QSA]
That's most flexible, straightforward, and portable solution.

.htaccess rewrite on mediatemple

I'm hosted on mediatemple DV 4.0 server and use the following lines in my htaccess file (rewrite rules for the CMS)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]
it works well but when I'm trying to do a 301 redirect to point the old page to a new address it adds some unnecessary stuff into the url, for instance:
?id=locations-maryland
How do I modify the rewrite rule to keep the same functionality but remove the last part which basically makes the 301 redirect pointless as the CMS couldn't not find any page with this spammy url.
It happens only on mediatemple I heard something about adding/removing an additional question mark somewhere but have not enough knowledge to resolve it by myself.
Add a ? to the end of the redirect target. Example:
Redirect /something/ /something/else/?
or if you're using mod_rewrite:
RewriteRule ^/?something/(.*)$ /something/else/$1? [L,R=301]

Resources