htaccess - does RewriteRule reflect the visible URL in the browser? - .htaccess

I would need some help with the .htaccess configuration to manage following problem.
An URL was entered in the browser "...somesite.com/en". The visible URL shall change to "...somesite.com/en/" after enter was hit, but still the files from the main www folder shall be served. I guess that two RewriteRules shall apply but not sure how to combine them - maybe with the [N] key?! Nothing worked for me so far...
RewriteCond %{REQUEST_URI} ^/[a-z]{2}$
RewriteRule ^([a-z]{2})$ $1/ [NC,N]
RewriteRule ^.*$ "" [NC]
I'd like to understand what is happening in the foreground (visible URL in the browser) and what is happening in the background (server URL) ... Could RewriteRule replace the URL in the browser (what the [R] key would do), but and in the same time serve different content?
Thanks!

Related

.htaccess redirect in drupal that excludes node edit page

Currently I have the following htaccess redirect rule that detects any path that looks like
mysite.com/node/xxx and redirects it to a .php script that, in turn, finds the URL of the page and redirects the user to it (e.g. mysite.com/page/page.html)
RewriteCond %{QUERY_STRING} ^(page=(31|1))?$ [NC]
RewriteRule ^node/?$ /? [L,NC,R=301]
RewriteRule ^(../)?node/([0-9]+)$ noderedirect.php?nid=$2 [L]
What I want, however, is that also when user accesses
mysite.com/node
or
mysite.com/node?page=xxx
they get redirected to the main page
BUT
when they access
mysite.com/node/xxx/edit
the rule doesn't get activated.
I tried several options (stopping at the one above) and so far I have this, but it still enables users to access mysite.com/node?page=xxx (all the other conditions are working fine.
Can somebody help?
I just want it to go to that page, and not to do anything. but when it's /node/xxx or /node or /node?page=xxx i want it to redirect to noderedirect.php
If you want it to always go to the noderedirect, then try:
RewriteCond %{THE_REQUEST} \ /+node\?page=([0-9]+)
RewriteRule ^ /node/%1? [L,R=301]
RewriteRule ^node/?$ /noderedirect.php [L]
RewriteRUle ^node/([0-9]+)/? /noderedirect.php?nid=$1 [L]

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]

Redirect /index.php from the end of the URL

Have a website with subpages all in this format:
mydomain.com/something
That's fine. But what is NOT fine is that you can also do mydomain.com/something/index.php (you can enter address in this format into your browser) and you still get the content on that mydomain.com/something.
I don't want those two possibilites to be available at the same time, Google doesn't like this. I want just one to be possible.
So what I want to do is whenever you type into your browser mydomain.com/something/index.php, you will be redirected to mydomain.com/something (without that /index.php at the end).
How should I write a .htaccess code to do something like this?
add the following lines to .htaccess in the root directory of your website
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.*?)/?index.php$ /$1 [R=301,L,NC,QSA]
Note: the first condition assure that no previous redirection is made (to prevent redirection loop)
Mordor:
You can try this in your .htaccess file:
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

Use htaccess to redirect root but still have directory listing accessible with a backdoor

What I thought I was trying to do was genius, but it seems to not work properly.
I have a public subdomain with temp files on it. I don't want people to be able to see all the files listed when they visit it. I don't want to password protect it because that will mess up several scripts when fetching files off the server, and I don't want a blank index.html there to stop listing because I want to be able to see the listing.
So my stroke of genius was to have a .htaccess file like so:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/#dir
RewriteCond %{REQUEST_URI} !/?show=dir
RewriteRule ^$ http://example.com/ [nc]
My theory was that visiting files.example.com would redirect to example.com but visiting files.example.com/?show=dir or files.example.com/#dir would not; and obviously since ?show=dir does nothing it would list all my files as normal.
My RewriteConditions have no effect though.
Is this possible?
First, you can't match against URL fragments (the #dir part) because that doesn't ever get sent to the server. It's a client side only thing.
Second, you can't match against the query string (the ?show=dir part) in the %{REQUEST_URI} variable, you need to use %{QUERY_STRING} instead.
So try:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^show=dir$
RewriteRule ^$ http://example.com/ [nc]

.htaccess redirect for blackberry not working

I am trying to redirect blackberry users to my mobile site by doing:
# redirect for blackberry users
RewriteCond %{HTTP_USER_AGENT} ^.BlackBerry.$
RewriteRule ^(.*)$ http://www.domain.com/m/ [R=301]
in my .htaccess, but nothing happens when I try to access from the device, I've already deleted cache and cookis and nothing works. I have been googling around and it seems I'm doing the redirect correctly but I guess not, what am I missing?
My .htaccess only contains that by the way.
Edit
The .htaccess in my server's root.
If this isn't the only rule in your .htaccess file, you might have an issue where a later rule messes up your redirect. To redirect immediately, you need to include the L flag.
I also suspect that your regular expression for the user agent is probably not correct for the input you're testing against, since the two . match just one character on either side of the word "BlackBerry". It would also be a good idea to guard against a redirect loop with a check to see if you're already in /m/ (although if you have mod_rewrite directives in a .htaccess file in that directory it's not important).
Putting all of that together, we get something like the following:
# Check for x-wap-profile/Profile headers
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile} !^$ [OR]
# Check for BlackBerry anywhere in the user agent string
RewriteCond %{HTTP_USER_AGENT} BlackBerry [NC]
# Make sure we're not in /m/ already
RewriteCond %{REQUEST_URI} !^/m/
RewriteRule ^ http://example.com/m/ [R=301,L]
You may also want that RewriteRule to be...
RewriteRule ^.*$ http://example.com/m/$0 [R=301,L]
...if the content is named the same (but mobile-friendly) in the /m/ directory.
Make sure your BlackBerry is not in "emulation mode" where it passes the user-agent for IE or Firefox instead of BlackBerry. You can check in the browser's option screen.
A better way around this would be to base your rewrite rule on the "x-wap-profile" and/or "profile" headers, which the mobile browser should always send accurately.

Resources