I need to rewrite all possible request to the path of the server
Example: If I enter https://wwww.myhost.com/#hi=random+text+1?stop=now
I need a redirect to:
http://myhost.com/
It is possible with url.rewrite?
Thanks
What are you really trying to achieve? Do you want to hide the urls so that it always shows http://myhost.com. I believe that is not possible if you have more than one page.
Are you trying to redirect everything to for example index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?anything=$1 [QSA,L]
This way for example http://myhost.com/example/ will be handled like the request was http://myhost.com/index.php?anything=example
Related
My self-coded portfolio website is giving me problems when used as an Instagram profile link (let's say www.myurl.com): the Instagram browser displays a 404 error when clicking the profile link. Clicking "Back to Index" on the 404 page shows the website as it should. After checking the URL, I see that Instagram automatically adds a random string to the domain (like www.myurl.com/?fbclid=PAAaa-Vm9q.../) and I assume my website doesn't know how to redirect this. The issue does not occur on a desktop computer, only mobile.
I'm guessing this random string is an SEO tool, which I woefully do not know how to use.
But I need the website link on IG to go through to my homepage without throwing the 404 error, at a minimum. Is there some kind of code / html document / web hosting / dns setting I need to manipulate to get this to work?
I have a feeling this is an issue caused by DNS settings on Cloudflare since I recently set up a proxy with them to use their global SSL certificate.
Some notes about my website in case they are helpful:
hosting on GoDaddy, recently proxied through CloudFlare for SSL certificate
I have the main domain (www.myurl.com) automatically redirect to a subdomain (sub.myurl.com). This subdomain is actually stored in a folder like www.myurl.com/sub/ .
Website is a CMS built on PHP 5
I tried URL shortener services as a stopgap measure. The only one that solved the problem was shorturl.at, but tiny.url and bit.ly still had the 404 issue. I have no idea why.
I looked at the htaccess document in the subdomain root folder, the contents are below. I think the issue may be that the "base" needs to be "rewritten" to the subfolder (www.myurl.com/sub/) but I'm not sure how to correctly change this code:
RewriteEngine on
# Some hosts require a rewritebase rule, if so, uncomment the RewriteBase line below. If you are running from a subdirectory, your rewritebase should match the name of the path to where stacey is stored.
# ie. if in a folder named 'stacey', RewriteBase /stacey
#RewriteBase /
ErrorDocument 404 /404.html
# Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]
# Rewrite any calls to /* or /app to the index.php file
RewriteCond %{REQUEST_URI} /app/$
RewriteRule ^app/ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?$1 [L]
# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]
I'm very much a beginner with webhosting / coding / etc so will appreciate an idiot-friendly explanation!! Thanks in advance.
This is my mod_rewrite code:
Options +FollowSymlinks
RewriteEngine on
# ————————————————————————-
# > URL REWRITING
# ————————————————————————-
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)/diario$ hotsite/diary.php
RewriteRule ^(.*)/recados$ hotsite/messages.php
RewriteRule ^(.*)/fotos$ hotsite/photos.php
RewriteRule ^(.*)/videos$ hotsite/videos.php
RewriteRule ^(.*)/contato$ hotsite/contact.php
RewriteRule ^([a-z0-9._\-]+)$ hotsite/index.php [L]
It works just like facebook profiles. Whenever I type "mywebsite.com/user.name" it goes to that user page. I can also type "mywebsite.com/user.name/videos" to go to a specific page in the user profile.
However, I can't access "mywebsite.com" anymore, because it redirects to "mywebsite.com/hotsite/index.php". How can I disable this behavior and leave the redirects only when someone types a user name at the end?
Thank you very much.
The RewriteCondition(s) only affect the RewriteRule that comes immediately after that. So, to stop redirecting htp://mywebsite.com/aDirThatExists like http://mywebsite.com/aboutUs you need to repeat the rewrite conditions as
RewriteCond %{SCRIPT_FILENAME} !-d # if not a directory
RewriteCond %{SCRIPT_FILENAME} !-f # and not a file
RewriteRule ^([a-z0-9._\-]+)$ hotsite/index.php [L]
This, however, should not affect a root URL request i.e. http://mywebsite.com/ because your regex clearly matches one or more characters after / because of the []+ plus sign.
I really need some help redirecting dirty URLs to clean ones.
Dirty URL: creature.php?beast=
Clean URL: /mythical-creature/
Currently my .htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mythicalcreatureslist.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www.mythbeasts.com [NC]
RewriteRule ^(.*)$ http://www.mythicalcreatureslist.com/$1 [L,R=301]
RewriteRule ^menu/([A-Za-z0-9-+]+)/?$ menu.php?menu=$1 [NC,L]
RewriteRule ^menu_two/([A-Za-z0-9-+]+)/?$ menu_two.php?menu=$1 [NC,L]
RewriteRule ^mythical-creature/([A-Za-z0-9-+\'%]+)/?$ creature.php?beast=$1 [NC,L]
What this does is it makes the website show clean URLs when browsing which is great. However the old dirty URLs do not redirect when typed in the URL bar.
Example: Mongolian Death Worm
http://www.mythicalcreatureslist.com/creature.php?beast=Mongolian+Death+Worm
I want it to redirect to:
http://www.mythicalcreatureslist.com/mythical-creature/Mongolian+Death+Worm
This is causing duplicate content. I have tried:
RewriteRule ^/mythical-creature/([A-Za-z0-9-+\'%]+)/?$ http://www.mythicalcreatureslist.com/creature.php?beast=$1 [R=301,NC,L]
But what that did was to cause the dirty one to be used all the time.
I then flipped it round:
RewriteRule ^creature.php?beast=([A-Za-z0-9-+\'%]+)/?$ http://www.mythicalcreatureslist.com/mythical-creature/$1 [R=301,NC,L]
But that just caused 404s whilst browsing and still did not redirect the old dirty URLs when typed in the address bar.
The regex did not not match an empty query parameter value. Flip the + to a *
RewriteRule ^/creature.php?beast=([A-Za-z0-9-+\'%]*)/?$ http:// www.mythicalcreatureslist.com/mythical-creature/$1 [R=301,NC,L]
Notice the addition of a leading slash also
I'm new to this community, but I've found it really useful time to time.
I've searched for the answer a lot, but I didn't find anything like I need. I've tried to solve it on my own, but I still get errors, so I hope to find someone that can show me the right way... :-)
I've got a "classic" ZF website, with many controller/action urls that are redirect to index.php with a .htaccess file.
Now, what I need, is to redirect a couple of controller to https ssl connection excluding some actions of both controllers.
The way I was trying to do it is:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.tld [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond $1 ^!((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond $1 ^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(js|ico|gif|jpg|png|css|swf|pdf|txt)$ index.php
It seems to work when I go to /member controller
But then, when I go to another controller, for example /index or /about it does not redirect to the http connection (port 80), and if I try to change a little the rewrite condition regex it sometimes does a redirect loop and the browser gives me a notice blocking the connection to the site.
Is there anyone that could show me the right synthax to use in my rewrite conditions to allow both the controllers (excluding the given actions) under an https connection and going back to a standard http connection when changing controller?
Thanks in advance.
Alessandro
Try these rules instead (replace appropriate lines):
RewriteCond %{HTTPS} on [NC]
RewriteCond %{REQUEST_URI} !^/(member|shop)/ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} ^/(member|shop)/ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
These rules a bit simple (HTTPS will be applied to ALL URLs in /member/ and /shop/ controllers (e.g. /member/login, /member/dashboard, /member/orders/15423/invoice etc)
Negate ! should be before ^ in RewriteCond directive -- if you want your own rules then replace RewriteCond $1 ^!((member|shop)/(?!(index|login))(.*)) by RewriteCond $1 !^((member|shop)/(?!(index|login))(.*))
A method we use to redirect to https is to leave the default Zend Framework .htaccess settings and create an action helper to redirect to https when required. Our action helper code is:
<?php
class RequireSSL extends Zend_Controller_Action_Helper_Abstract
{
public function direct()
{
if(empty($_SERVER['HTTPS']) && $config['billing']['requireSSL'])
{
$redirector = $this->getActionController()->getHelper('Redirector');
$redirector->goToUrlAndExit('https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
}
}
Then when you are in a controller action that you need to be accessed via HTTPS you simply call this function at the beginning:
$this->_helper->requireSSL();
Also, here is another method for using action helpers that is a little more detailed if you need it:
http://juriansluiman.nl/en/article/110/in-control-of-https-for-action-controllers
Hope that helps!
I have discovered where the origin of the problem is, but I'd still need support to understand how to solve it.
I have tested on a local linux machine the htaccess and the result was the same... testing separately the two https condition statements (on and off) they work correctly redirecting basing on the given RewriteCond regex. When putting together only the redirect from http to https works.
Https to http redirect works only if the regex is not matched, else it redirects to http://www.mydomain.tld/index.php
So I finally tried to delete the last htaccess statement and it started to work correctly, but, obviously, it does not find the url, as it does not redirect to the index.php anymore.
It looks like after the correct https redirect the index.php one creates the problem. So I'm asking myself if there is a way to avoid this and make it work correctly.
As I wrote before, this seems to be a common problem of this htaccess, as its behaviour is the same on the test and on the production server (different linux flavours).
I put here the working code:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteCond %{HTTP_HOST} ^mydomain\.tld [NC]
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond $1 !^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) http://%{HTTP_HOST}/$1 [L,R=302]
RewriteCond %{HTTPS} off
RewriteCond $1 ^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=302]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule !\.(js|ico|gif|jpg|png|css|swf|pdf|txt)$ index.php
Finally I have solved it! I persevered in searching the answer because I think that doing it with .htaccess is cleaner, smarter and easy to maintain.
The problem was essentially due to the regexp used in the "ssl to non-ssl" block that was not correctly matching the value passed (that is best matched now reading the env variable %{THE_REQUEST}, avoiding, in some cases, an erroneus redirects loop.
I paste here the working code for further reference:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteOptions MaxRedirects=1
RewriteCond %{HTTP_HOST} ^yoursite\.tld [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /((controller1|controller2)/(?!(action1|action2))(.*))\ HTTP/
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /((controller1|controller2)/(?!(action1|action2))(.*))\ HTTP/
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} \.(js|css|ico|gif|jpg|png|swf|txt|pdf)$ [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
I'm trying to whitelabel the Zend framework, so I've created folder in application called Site.
This url: http://local.branch.test.com/site-css/1/layout.css returns a 400 Bad Request.
With the following rule:
RewriteRule ^site-css/([0-9]+)/(.*)$ ../application/site/$1/design/css/$2 [L]
Full .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php
RewriteRule ^site-css/([0-9]+)/(.*)$ ../application/site/$1/design/css/$2 [L]
Ideas?
Try
RewriteBase /
RewriteRule ^site-css/([0-9]+)/(.*)$ ../application/site/$1/design/css/$2 [L]
This is caused by apache being set up to point to /public. The project doesn't have access to read files outside of its root.
Therefore I have moved site into public.